Repository navigation
Description
Describe the bug
When an entrypoint generator yields an object that json.dumps cannot serialise, BedrockAgentCoreApp._safe_serialize_to_json_string (runtime/app.py line 898 in 1.22.0) tries convert_complex_objects and then falls back to json.dumps(str(obj)). convert_complex_objects (runtime/utils.py) handles Pydantic models, dataclasses, dicts, lists, tuples and sets but not bytes, so any event that contains binary data anywhere in its tree reaches the third fallback. The SSE data: line then carries a JSON string holding the Python repr of the dictionary, for example "{'result': {'type': 'agent_result', 'message': {... b'rsn_...'}}}", rather than a JSON object or an error.
The client cannot tell this apart from a legitimate string event, and it cannot recover the original payload from the repr. In our case a supervisor agent consuming a sub-agent's stream did event.get("error") on what was now a str and failed with AttributeError: 'str' object has no attribute 'get', turning every affected turn into a user-facing failure. Nothing was logged on the producing side, because the fallback succeeds.
To Reproduce
- Write a streaming entrypoint whose generator yields a dictionary containing a
bytesvalue somewhere inside it. A real-world source is theAgentResult.to_dict()from Strands when the model returns areasoningContent.redactedContentblock, which OpenAI GPT-5.6 Luna on Bedrock does after tool calls. Any otherbytesvalue in a yielded event, such as image or documentsource.bytesfrom a tool result, takes the same path. - Invoke the runtime and read the SSE stream.
- Observe that the event arrives as
data: "{'result': ...}"(a JSON string) instead ofdata: {"result": ...}.
Expected behavior
One of:
- binary values are converted to something JSON can carry (for example base64), or
- the event is replaced by an explicit error event, as
_stream_with_error_handlingalready does for exceptions, and a warning is logged.
Either way the consumer should never receive a Python repr as the payload.
Additional context
- Versions: bedrock-agentcore 1.22.0 (latest on PyPI), Python 3.13, Strands 1.54.0, runtime container on Bedrock AgentCore.
- The final fallback that logs a warning only fires if
json.dumps(str(obj))itself fails, which it never does for a dictionary. - Related: Bug: message_to_payload fails with 'Object of type bytes is not JSON serializable' when message contains image bytes #198 (closed) reported bytes-in-message failing in the memory converter; this is the same class of input on the response-streaming path.
- Companion report about
AgentResult.to_dict()not being JSON-serialisable: [BUG] AgentResult.to_dict() is not JSON-serialisable when the message contains bytes (e.g. reasoningContent.redactedContent) strands-agents/harness-sdk#4167.
Activity
kimnamu commentedon Sep 22, 2026
Thanks for the careful report, and for tracing the SSE payload all the way down to the third fallback in
_safe_serialize_to_json_string; that made this quick to confirm.Reproduced on
main(889615f, v1.23.1) with the StarletteTestClient.runtime/utils.pyandruntime/app.pyare unchanged between v1.22.0 andmain, so the released versions behave the same. The cause is thatconvert_complex_objects(src/bedrock_agentcore/runtime/utils.py:7-35) has no branch forbytes, so the second attempt'sjson.dumpsraisesTypeErrorandapp.py:919json.dumps(str(obj))succeeds without logging. Adding abytesbranch next to the existingsetbranch (base64, the same choice strands-agents/harness-sdk#4313 made forAgentResult.to_dict()) turns the event back into a JSON object, and the existing runtime tests stay green.main889615fdata:line"{'result': {... b'rsn_abc'}}}"(JSON string of the repr){"result": {... "redactedContent": "cnNuX2FiYw=="}}json.loads(...)str, thenAttributeError: 'str' object has no attribute 'get'dictruntime/test_utils.py+runtime/test_app.pyRepro (
repro.py, no AWS credentials needed):Output on
main:Minimal fix, mirroring the sibling branches in
convert_complex_objects:Same script after the patch:
Regression test (red on main, green with the branch), ruff, and notes on the alternatives
Added next to
test_setsintests/bedrock_agentcore/runtime/test_utils.py(plusimport jsonat the top):Notes, in case they help with the decision:
Responsepath atapp.py:600, which goes through the same helper. The other route, dropping thejson.dumps(str(obj))attempt so the existing warning plus{"error": "Serialization failed"}fallback fires, would also change thedatetime/Decimal/frozensetresults thattest_safe_serialize_fallback_to_stringcurrently expects asstr(obj).strands/types/session.py) wraps it as{"__bytes_encoded__": true, "data": ...}so consumers can decode it back; if that shape is preferred here, it is a one-line change in the same branch.AgentResult.to_dict()encode bytes itself, so theredactedContenttrigger goes away once agents pick up that release. Otherbytesin a yielded event, such as image or documentsource.bytesin a tool result, still reach this fallback, so the SDK-side branch seems useful regardless.If a PR is welcome for this despite the current external-PR policy, I can open one with the diff and test above; otherwise please feel free to take it as-is.