The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(core): allow unknown blocks in convert_to_openai_messages (#34420)
ccurme6519a5675b24
diff --git a/libs/core/langchain_core/messages/utils.py b/libs/core/langchain_core/messages/utils.pyindex 95f9221c53..6ea7b53b04 100644--- a/libs/core/langchain_core/messages/utils.py+++ b/libs/core/langchain_core/messages/utils.py@@ -1049,6 +1049,7 @@ def convert_to_openai_messages(*,text_format: Literal["string", "block"] = "string",include_id: bool = False,+ pass_through_unknown_blocks: bool = True,) -> dict | list[dict]:"""Convert LangChain messages into OpenAI message dicts.@@ -1068,6 +1069,9 @@ def convert_to_openai_messages(content blocks these are left as is.include_id: Whether to include message IDs in the openai messages, if theyare present in the source messages.+ pass_through_unknown_blocks: Whether to include content blocks with unknown+ formats in the output. If `False`, an error is raised if an unknown+ content block is encountered.Raises:ValueError: if an unrecognized `text_format` is specified, or if a message@@ -1317,6 +1321,36 @@ def convert_to_openai_messages(},})+ elif block.get("type") == "function_call": # OpenAI Responses+ if not any(+ tool_call["id"] == block.get("call_id")+ for tool_call in cast("AIMessage", message).tool_calls+ ):+ if missing := [+ k+ for k in ("call_id", "name", "arguments")+ if k not in block+ ]:+ err = (+ f"Unrecognized content block at "+ f"messages[{i}].content[{j}] has 'type': "+ f"'tool_use', but is missing expected key(s) "+ f"{missing}. Full content block:\n\n{block}"+ )+ raise ValueError(err)+ oai_msg["tool_calls"] = oai_msg.get("tool_calls", [])+ oai_msg["tool_calls"].append(+ {+ "type": "function",+ "id": block.get("call_id"),+ "function": {+ "name": block.get("name"),+ "arguments": block.get("arguments"),+ },+ }+ )+ if pass_through_unknown_blocks:+ content.append(block)elif block.get("type") == "tool_result":if missing := [k for k in ("content", "tool_use_id") if k not in block@@ -1397,7 +1431,10 @@ def convert_to_openai_messages(},})- elif block.get("type") in ["thinking", "reasoning"]:+ elif (+ block.get("type") in ["thinking", "reasoning"]+ or pass_through_unknown_blocks+ ):content.append(block)else:err = (diff --git a/libs/core/tests/unit_tests/messages/test_utils.py b/libs/core/tests/unit_tests/messages/test_utils.pyindex 0d526e4ded..fbf949e9bb 100644--- a/libs/core/tests/unit_tests/messages/test_utils.py+++ b/libs/core/tests/unit_tests/messages/test_utils.py@@ -1197,7 +1197,47 @@ def test_convert_to_openai_messages_guard_content() -> None:def test_convert_to_openai_messages_invalid_block() -> None:messages = [HumanMessage(content=[{"type": "invalid", "foo": "bar"}])]with pytest.raises(ValueError, match="Unrecognized content block"):- convert_to_openai_messages(messages, text_format="block")+ convert_to_openai_messages(+ messages,+ text_format="block",+ pass_through_unknown_blocks=False,+ )+ # Accept by default+ result = convert_to_openai_messages(messages, text_format="block")+ assert result == [{"role": "user", "content": [{"type": "invalid", "foo": "bar"}]}]+++def test_handle_openai_responses_blocks() -> None:+ blocks: str | list[str | dict] = [+ {"type": "reasoning", "id": "1"},+ {+ "type": "function_call",+ "name": "multiply",+ "arguments": '{"x":5,"y":4}',+ "call_id": "call_abc123",+ "id": "fc_abc123",+ "status": "completed",+ },+ ]+ message = AIMessage(content=blocks)++ expected_tool_call = {+ "type": "function",+ "function": {+ "name": "multiply",+ "arguments": '{"x":5,"y":4}',+ },+ "id": "call_abc123",+ }+ result = convert_to_openai_messages(message)+ assert isinstance(result, dict)+ assert result["content"] == blocks+ assert result["tool_calls"] == [expected_tool_call]++ result = convert_to_openai_messages(message, pass_through_unknown_blocks=False)+ assert isinstance(result, dict)+ assert result["content"] == [{"type": "reasoning", "id": "1"}]+ assert result["tool_calls"] == [expected_tool_call]def test_convert_to_openai_messages_empty_message() -> None: