High severity intermediate · Fix: 5-10 min

ValueError

aws_bedrock_runtime.InvokeModelWithResponseStream parse error (ValueError)

What this error means
AWS Bedrock's InvokeModelWithResponseStream API returned a response stream that failed JSON or event stream parsing in the client.

Stack trace

traceback
Traceback (most recent call last):
  File "app.py", line 42, in <module>
    response = client.invoke_model_with_response_stream(...)
  File "aws_bedrock_runtime/client.py", line 128, in invoke_model_with_response_stream
    for event in parse_response_stream(response):
  File "aws_bedrock_runtime/parsing.py", line 56, in parse_response_stream
    data = json.loads(event.data)
ValueError: Expecting value: line 1 column 1 (char 0)
QUICK FIX
Add filtering logic to skip empty or non-JSON events before parsing the response stream JSON.

Why it happens

The AWS Bedrock streaming response contains event stream messages that must be parsed as JSON objects. If the stream includes unexpected empty messages, malformed JSON, or non-JSON control events, the JSON parser raises a ValueError. This often happens when the client code does not properly handle or filter event stream control frames or incomplete chunks.

Detection

Monitor logs for ValueError exceptions during InvokeModelWithResponseStream calls and log raw event stream data to identify malformed or unexpected messages before parsing.

Causes & fixes

1

The response stream includes empty or heartbeat events with no JSON payload.

✓ Fix

Filter out empty or control events before calling json.loads() on event data.

2

Partial or truncated JSON data chunks are passed to the JSON parser.

✓ Fix

Buffer and concatenate stream chunks until a complete JSON object is received before parsing.

3

The client code assumes all events contain JSON data, but some events are metadata or control frames.

✓ Fix

Implement event type checking and parse only events with JSON payloads.

Code: broken vs fixed

Broken - triggers the error
python
from aws_bedrock_runtime import BedrockClient

client = BedrockClient()
response = client.invoke_model_with_response_stream(modelId="my-model", input={"prompt": "Hello"})
for event in response:
    data = json.loads(event.data)  # ValueError here if event.data is empty or invalid
    print(data)
Fixed - works correctly
python
import os
import json
from aws_bedrock_runtime import BedrockClient

client = BedrockClient()
response = client.invoke_model_with_response_stream(modelId="my-model", input={"prompt": "Hello"})
for event in response:
    if event.data and event.data.strip():  # Skip empty events
        try:
            data = json.loads(event.data)
            print(data)
        except json.JSONDecodeError:
            # Log and skip malformed JSON event
            print(f"Skipping malformed event data: {event.data}")
Added filtering to skip empty event data and wrapped json.loads() in try/except to handle malformed JSON gracefully.

Workaround

Wrap the JSON parsing in try/except blocks and skip or log events that fail parsing to avoid crashing the stream processing.

Prevention

Use the AWS Bedrock SDK's built-in event stream parsers or utilities that correctly handle control frames and partial messages to ensure only valid JSON events are parsed.

Python 3.9+ · aws-bedrock-runtime >=1.0.0 · tested on 1.2.0
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.