BerriAI/litellm · error · ValueError
Request must contain content to count tokens
Error message
Request must contain content to count tokens
What it means
Raised when a count-tokens request is detected as InvokeModel format (not messages-based) but contains nothing besides the 'model' field (len(request_data) <= 1). InvokeModel bodies vary per model family, so litellm only enforces that some content field (inputText, prompt, etc.) exists — a body carrying only 'model' gives the tokenizer nothing to count.
Solutions
- Add the content field your model expects, e.g. {'model': 'bedrock/titan...', 'inputText': 'hello'}
- If you intended Converse format, send 'messages': [...] — its presence switches the request to the Converse branch
- Log the final request body right before the call to catch over-aggressive field stripping
Example fix
# before
body = {'model': 'bedrock/amazon.titan-embed-text-v2:0'}
# after
body = {'model': 'bedrock/amazon.titan-embed-text-v2:0', 'inputText': 'hello world'} Defensive patterns
Strategy: validation
Validate before calling
def validate_invoke_model_body(body: dict) -> None:
if 'messages' not in body and len([k for k in body if k != 'model']) == 0:
raise ValueError(
'InvokeModel count_tokens body needs a content field (e.g. inputText) besides model'
) Try / catch
try:
handler.count_tokens(body)
except ValueError as e:
if 'must contain content' in str(e):
body['inputText'] = body.pop('text', '') or DEFAULT_TEXT
handler.count_tokens(body)
else:
raise Prevention
- Decide Converse vs InvokeModel shape per provider and centralize it
- Log the final body keys before sending to catch stripped fields
- Template bodies should fail loudly when placeholders render empty
When it happens
Trigger: Sending {'model': 'bedrock/...'} alone; sending {'model': ..., 'metadata': {...}}-style wrappers whose real content field was dropped; mistakenly using an InvokeModel shape when you meant to send Converse messages.
Common situations: Stripping fields while debugging until only model remains; building the body from a config template whose content placeholder is empty; provider format confusion between Converse and InvokeModel styles.
Understand the failure class
Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.
Related errors
- Message must be a dictionary
- Message must have a 'content' field
- Message must have a 'role' field
- messages must be a list
- messages parameter is required for Converse input
AI-assisted analysis of BerriAI/litellm@77b7c6c40c (2026-08-18).
Data as JSON: /api/errors/71858fff915265b6.
Report an issue: GitHub.
Appendix: source
Thrown at litellm/llms/bedrock/count_tokens/transformation.py:282
raise ValueError("messages parameter is required for Converse input")
if not isinstance(messages, list):
raise ValueError("messages must be a list")
for i, message in enumerate(messages):
if not isinstance(message, dict):
raise ValueError(f"Message {i} must be a dictionary")
if "role" not in message:
raise ValueError(f"Message {i} must have a 'role' field")
if "content" not in message:
raise ValueError(f"Message {i} must have a 'content' field")
else:
# For InvokeModel format, we need at least some content to count tokens
# The content structure varies by model, so we do minimal validation
if len(request_data) <= 1: # Only has 'model' field
raise ValueError("Request must contain content to count tokens")
View on GitHub (pinned to 77b7c6c40c)