{"record":{"id":"c89d2a6b18bccbbc","repo":"langchain-ai/langchain","slug":"unrecognized-content-block-at-messages-i-conten-c89d2a","errorCode":null,"errorMessage":"Unrecognized content block at messages[{i}].content[{j}] has key 'image', but 'image' is missing expected key(s) {missing}. Full content block:\n\n{block}","messagePattern":"Unrecognized content block at messages\\[(.+?)\\]\\.content\\[(.+?)\\] has key 'image', but 'image' is missing expected key\\(s\\) (.+?)\\. Full content block:\n\n(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/messages/utils.py","lineNumber":1761,"sourceCode":"                                    \"url\": (\n                                        f\"data:{source['media_type']};\"\n                                        f\"{source['type']},{source['data']}\"\n                                    )\n                                },\n                            }\n                        )\n                    # Bedrock converse\n                    elif image := block.get(\"image\"):\n                        if missing := [\n                            k for k in (\"source\", \"format\") if k not in image\n                        ]:\n                            err = (\n                                f\"Unrecognized content block at \"\n                                f\"messages[{i}].content[{j}] has key 'image', \"\n                                f\"but 'image' is missing expected key(s) \"\n                                f\"{missing}. Full content block:\\n\\n{block}\"\n                            )\n                            raise ValueError(err)\n                        b64_image = _bytes_to_b64_str(image[\"source\"][\"bytes\"])\n                        content.append(\n                            {\n                                \"type\": \"image_url\",\n                                \"image_url\": {\n                                    \"url\": (\n                                        f\"data:image/{image['format']};base64,{b64_image}\"\n                                    )\n                                },\n                            }\n                        )\n                    else:\n                        err = (\n                            f\"Unrecognized content block at \"\n                            f\"messages[{i}].content[{j}] has 'type': 'image' \"\n                            f\"but does not have a 'source' or 'image' key. Full \"\n                            f\"content block:\\n\\n{block}\"\n                        )","sourceCodeStart":1743,"sourceCodeEnd":1779,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/messages/utils.py#L1743-L1779","documentation":"Thrown by `convert_to_openai_messages` in `langchain_core.messages.utils` when a content block uses the Bedrock Converse image shape (`{\"type\": \"image\", \"image\": {...}}`) but the inner `image` dict is missing required keys. The converter requires both `source` (with `bytes`) and `format` before it can build an OpenAI `image_url` data-URI block, and it refuses to guess. The offending block is echoed in the message so you can see exactly which keys are absent.","triggerScenarios":"Passing a message whose content contains `{\"type\": \"image\", \"image\": {\"format\": \"png\"}}` (no `source`), or `{\"image\": {\"source\": {\"bytes\": b\"...\"}}}` (no `format`), to `convert_to_openai_messages()`. Also triggered when hand-building Bedrock Converse payloads or relaying raw Bedrock output that was truncated/mutated before conversion.","commonSituations":"Porting prompts written for `boto3` Bedrock `converse` into LangChain/LangGraph pipelines; constructing image blocks from dicts where the base64 bytes were placed at the top level instead of under `source.bytes`; partial serialization that drops nested keys.","solutions":["Make the image block match Bedrock Converse shape: `{\"type\": \"image\", \"image\": {\"format\": \"png\"|\"jpeg\"|\"gif\"|\"webp\", \"source\": {\"bytes\": <raw bytes or b64 str>}}}`.","Or convert to native OpenAI shape instead: `{\"type\": \"image_url\", \"image_url\": {\"url\": \"data:image/png;base64,...\"}}`, which the converter passes through without key checks.","If relaying messages from an external system, validate/repair blocks in a pre-processing step before calling `convert_to_openai_messages`.","Wrap the call in try/except ValueError to surface which message index (i, j) is malformed and fail that request with context."],"exampleFix":"// before\ncontent = [{\"type\": \"image\", \"image\": {\"format\": \"png\", \"bytes\": raw}}]\n\n// after\ncontent = [{\n    \"type\": \"image\",\n    \"image\": {\"format\": \"png\", \"source\": {\"bytes\": raw}},\n}]\n// or OpenAI native:\ncontent = [{\"type\": \"image_url\", \"image_url\": {\"url\": f\"data:image/png;base64,{b64}\"}}]","handlingStrategy":"validation","validationCode":"def valid_bedrock_image(block: dict) -> bool:\n    img = block.get(\"image\")\n    return (\n        isinstance(img, dict)\n        and \"format\" in img\n        and isinstance(img.get(\"source\"), dict)\n        and \"bytes\" in img[\"source\"]\n    )\n\n# before conversion\nfor i, m in enumerate(messages):\n    for j, b in enumerate(m.content if isinstance(m.content, list) else []):\n        if b.get(\"type\") == \"image\" and \"image\" in b and not valid_bedrock_image(b):\n            raise ValueError(f\"malformed image block at messages[{i}].content[{j}]: {b}\")","typeGuard":null,"tryCatchPattern":"try:\n    oai = convert_to_openai_messages(messages)\nexcept ValueError as e:\n    # error text names messages[i].content[j]; use it to locate and repair the block\n    log.error(\"conversion failed: %s\", e)\n    raise","preventionTips":["Centralize image-block construction in one helper that always emits the full Bedrock shape","Never round-trip image blocks through lossy JSON that can drop nested keys","Write unit tests asserting the exact block shape before calling convert_to_openai_messages"],"tags":["messages","content-blocks","bedrock","openai-conversion","validation"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}