Error code: 400 - {'error': {'type': '<nil>', 'message': 'InvokeModelWithRespons...
🚨 错误信息
Model call failed after 4 attempts with BadRequestError: Error code: 400 - {'error': {'type': '<nil>', 'message': 'InvokeModelWithResponseStream: operation error Bedrock Runtime: InvokeModelWithResponseStream, https response error StatusCode: 400, RequestID: 5373cb4a-0043-4643-93bb-d94891d8c91a, ValidationException: Input is too long for requested model. (request id: 20260205201941826253952S9PdkIT1)'}, 'type': 'error'}
🤖 AI解决方案
报错分析报告错误类型:
失败尝试:4次重试后仍失败
模型平台:AWS Bedrock Runtime
具体问题:输入内容超过了模型的最大输入长度限制
API调用前预检查:在发送请求前计算输入token数
使用官方文档:查阅AWS Bedrock文档了解各模型的具体限制
配置超时和重试策略:避免无限重试导致资源浪费
Token计数工具:使用模型对应的tokenizer库(如HuggingFace Transformers)
监控日志:启用AWS CloudWatch监控Bedrock API调用
错误处理包装器:实现统一的错误处理和重试逻辑
##
原文:Input is too long for requested model
译文:输入内容对于请求的模型来说太长
原因分析
这是一个AWS Bedrock服务返回的400错误。根本原因是:输入文本的长度超过了目标AI模型支持的最大令牌(token)限制。
具体分析:
ValidationException##### 方案1:截断输入内容
python
如果使用Python,可以先截断输入文本
def truncate_text(text, max_tokens=4096):
"""
简单的截断函数,实际应根据模型的tokenizer调整
max_tokens: 根据具体模型的最大输入限制设置
常见模型限制:Claude-2 (100k), Llama-2 (4k), Titan (8k)
"""
# 简单按字符截断(实际应使用tokenizer)
max_chars = max_tokens * 4 # 粗略估算
return text[:max_chars]或使用更精确的token计数
import tiktoken # OpenAI的tokenizerdef count_tokens(text, model_name="claude-2"):
encoding = tiktoken.get_encoding("claude")
return len(encoding.encode(text))
方案2:分块处理长文本
python
def chunk_text(text, chunk_size=2000):
"""将长文本分成多个块处理"""
words = text.split()
chunks = []
current_chunk = []
current_length = 0
for word in words:
if current_length + len(word) + 1 <= chunk_size:
current_chunk.append(word)
current_length += len(word) + 1
else:
chunks.append(' '.join(current_chunk))
current_chunk = [word]
current_length = len(word)
if current_chunk:
chunks.append(' '.join(current_chunk))
return chunks
##措施
输入长度检查
2.推荐工具
---
提示:请先确认您使用的具体Bedrock模型类型(如Claude, Llama, Titan等),并查阅其官方文档中的最大输入长度限制。