{"record":{"id":"88627320296f74bf","repo":"BerriAI/litellm","slug":"agentcore-failed-to-read-parse-json-response-body","errorCode":null,"errorMessage":"AgentCore: Failed to read/parse JSON response body: {e}","messagePattern":"AgentCore: Failed to read/parse JSON response body: (.+?)","errorType":"exception","errorClass":"BedrockError","httpStatus":null,"severity":"error","filePath":"litellm/llms/bedrock/chat/agentcore/transformation.py","lineNumber":687,"sourceCode":"        # LOGGING\n        logging_obj.post_call(\n            input=messages,\n            api_key=\"\",\n            original_response=\"first stream response received\",\n            additional_args={\"complete_input_dict\": data},\n        )\n\n        # Check if response is JSON (agent used sync return) instead of SSE\n        content_type: Final = response.headers.get(\"content-type\", \"\").lower()\n        if \"application/json\" in content_type:\n            verbose_logger.debug(\n                \"AgentCore streaming: received JSON response instead of SSE, converting to single-chunk stream\"\n            )\n            try:\n                body: Final = response.read()\n                response_json: Final = json.loads(body)\n            except (json.JSONDecodeError, Exception) as e:\n                raise BedrockError(\n                    status_code=response.status_code,\n                    message=f\"AgentCore: Failed to read/parse JSON response body: {e}\",\n                )\n            parsed: Final = self._parse_json_response(response_json)\n\n            def _json_as_sync_stream():\n                # Content chunk\n                content_chunk: Final = ModelResponseStream(\n                    id=f\"chatcmpl-{uuid.uuid4()}\",\n                    created=0,\n                    model=model,\n                    object=\"chat.completion.chunk\",\n                )\n                content_chunk.choices = [\n                    StreamingChoices(\n                        finish_reason=None,\n                        index=0,\n                        delta=Delta(content=parsed[\"content\"], role=\"assistant\"),","sourceCodeStart":669,"sourceCodeEnd":705,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/llms/bedrock/chat/agentcore/transformation.py#L669-L705","documentation":"During synchronous AgentCore streaming, if the response Content-Type is application/json (the agent returned synchronously instead of via SSE), the handler reads the body with response.read() and json.loads it. Failure of either step raises BedrockError carrying the upstream status code — the endpoint promised JSON but the bytes were empty or unparseable.","triggerScenarios":"AgentCore returns Content-Type: application/json with an empty body (common on some error statuses); the connection drops mid-body so read()/loads see truncated bytes; an intermediate proxy emits a JSON content-type on a non-JSON error page.","commonSituations":"Long-running agents hitting idle or proxy timeouts that truncate the body; intermittent 5xx from the AgentCore runtime; API gateways rewriting error responses; older LiteLLM versions with weaker JSON-mode handling.","solutions":["Catch BedrockError and branch on status_code — 5xx/truncation cases are retryable with backoff","Increase client/stream timeouts and disable aggressive proxy buffering for amazonaws.com","Retry the request once or twice with exponential backoff; if it persists, reproduce with a minimal prompt","Update LiteLLM — robustness of the JSON-instead-of-SSE path has improved across releases"],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"from litellm.exceptions import BedrockError\n\nfor attempt in range(3):\n    try:\n        chunks = list(litellm.completion(model=agentcore_model, messages=msgs, stream=True))\n        break\n    except BedrockError as e:\n        if e.status_code is not None and e.status_code >= 500 and attempt < 2:\n            time.sleep(2 ** attempt)\n            continue\n        raise","preventionTips":["Set generous timeouts for agent workloads that may return large JSON bodies","Disable proxy response buffering for amazonaws.com","Keep LiteLLM current to benefit from JSON-mode parsing fixes"],"tags":["bedrock","agentcore","streaming","json","response-parsing"],"backgroundTag":"invalid-json-response","analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}