ValueError
builtins.ValueError
Stack trace
Traceback (most recent call last):
File "app.py", line 42, in <module>
response = client.bedrock_runtime.invoke_model(
File "/usr/local/lib/python3.9/site-packages/boto3/client.py", line 530, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 984, in _make_api_call
raise error_class(parsed_response['Error']['Message'])
ValueError: Expecting value: line 1 column 1 (char 0) Why it happens
The AWS Bedrock converse API expects the 'toolInput' parameter to be a valid JSON string. If the input is malformed, empty, or not properly serialized, the API returns an error when attempting to parse it. Similarly, if the response contains unexpected or invalid JSON, parsing fails. This often happens when the developer passes a Python dict directly without JSON serialization or includes extra characters.
Detection
Validate and log the 'toolInput' JSON string before sending the request. Use try/except around JSON serialization and API calls to catch ValueError and log raw inputs and outputs for debugging.
Causes & fixes
Passing a Python dict directly as 'toolInput' instead of a JSON string
Serialize the dict to a JSON string using json.dumps() before passing it to the 'toolInput' parameter.
Including trailing commas or comments in the JSON string causing invalid JSON
Ensure the JSON string is strictly valid by removing trailing commas and comments before sending.
Empty or None value passed as 'toolInput' causing parsing failure
Always pass a non-empty valid JSON string; if no input is needed, pass '{}' as the JSON string.
Response from Bedrock contains unexpected non-JSON content or is empty
Add error handling to check response content before JSON parsing and log raw response for troubleshooting.
Code: broken vs fixed
import boto3
client = boto3.client('bedrock-runtime')
input_data = {'query': 'Hello'}
response = client.invoke_model(
modelId='my-model',
contentType='application/json',
accept='application/json',
body={"toolInput": input_data} # This line causes ValueError
)
print(response) import os
import json
import boto3
client = boto3.client('bedrock-runtime',
aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
region_name=os.environ.get('AWS_REGION', 'us-east-1'))
input_data = {'query': 'Hello'}
json_input = json.dumps(input_data) # Serialize dict to JSON string
response = client.invoke_model(
modelId='my-model',
contentType='application/json',
accept='application/json',
body={"toolInput": json_input} # Fixed: pass JSON string
)
print(response) Workaround
Wrap the API call in try/except catching ValueError; on failure, log the raw 'toolInput' and response content, then manually parse JSON with error-tolerant methods or retry with corrected input.
Prevention
Always serialize tool input data to valid JSON strings before API calls and validate JSON format with linters or schema validators to avoid malformed payloads.