AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Convert_to_openai_messages Raises KeyError 'Type' For Content Blocks Without A Type Key

The default `text_format='string'` path in `convert_to_openai_messages` directly accesses `block["type"]` while collapsing content blocks, causing a `KeyError` for legitimate Bedrock Converse content blocks that lack a `type` key. The same blocks pass through correctly when `text_format='block'` is used, violating the promise of `pass_through_unknown_blocks=True`.

highConfidence 98%Langchain-CoreMemoryVector StoresRetrieversAffected V1.6.6

Origin Analysis

In `langchain_core/messages/utils.py`, the string-collapsing branch uses `block["type"] != "text"` instead of the defensive `block.get("type") != "text"`. Content blocks from Bedrock Converse (e.g., `{"text": "a"}`) lack a `type` key, so direct subscripting raises `KeyError` before the block can be passed through.
1. Install langchain-core 1.6.6 (or current master). 2. Run: ```python from langchain_core.messages import AIMessage from langchain_core.messages.utils import convert_to_openai_messages message = AIMessage(content=[{"text": "a"}]) convert_to_openai_messages([message]) ``` 3. Observe `KeyError: 'type'` raised from `utils.py:1969`.

Fixing Code Block

Edge Case Audit

The fix resolves the KeyError for missing `type`, but if a block has `type == "text"` and is missing the `"text"` key, the subsequent `block["text"]` access will still raise `KeyError`. This edge is pre-existing and not addressed. Additionally, for previously crashing inputs, the output type changes from an exception to a list of blocks instead of a string; downstream code expecting a string may need to handle list content. No concurrency or threading concerns. To roll back, simply revert to the original line using `block["type"]`; no schema or migration changes are required.

Ecosystem Topology

Agent Frameworks

LangChain, LlamaIndex, and Pydantic

  1. highProperty graph relation keys collide when entity IDs or labels contain underscores
  2. highCodeActAgent drops assistant tool-call metadata during handoff, leaving an orphan tool response
  3. high[Bug]: HierarchicalNodeParser async methods return the documents unsplit
  4. highLLM.predict_and_call fails with ValidationError for non-function-calling models when react_chat_formatter is omitted
  5. highAnthropic streaming server-tool input_json_delta overwrites completed client tool_use arguments due to stale cur_tool_call state