The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(openai): handle missing 'text' key in responses API content blocks (#34198)
j3r0lin5720dea41b5d
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.pyindex 48bbfcca04..5e21e4d0bc 100644--- a/libs/partners/openai/langchain_openai/chat_models/base.py+++ b/libs/partners/openai/langchain_openai/chat_models/base.py@@ -4048,9 +4048,14 @@ def _construct_responses_api_input(messages: Sequence[BaseMessage]) -> list:if block_type in ("text", "output_text", "refusal"):msg_id = block.get("id")if block_type in ("text", "output_text"):+ # Defensive check: block may not have "text" key+ text = block.get("text")+ if text is None:+ # Skip blocks without text content+ continuenew_block = {"type": "output_text",- "text": block["text"],+ "text": text,"annotations": [_format_annotation_from_lc(annotation)for annotation in block.get("annotations") or []diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_base.py b/libs/partners/openai/tests/unit_tests/chat_models/test_base.pyindex f9a9afc67c..54f2f961a3 100644--- a/libs/partners/openai/tests/unit_tests/chat_models/test_base.py+++ b/libs/partners/openai/tests/unit_tests/chat_models/test_base.py@@ -2091,6 +2091,38 @@ def test__construct_responses_api_input_multiple_message_components() -> None:]+def test__construct_responses_api_input_skips_blocks_without_text() -> None:+ """Test that blocks without 'text' key are skipped."""+ # Test case: block with type "text" but missing "text" key+ messages = [+ AIMessage(+ content=[+ {"type": "text", "text": "valid text", "id": "msg_123"},+ {"type": "text", "id": "msg_123"}, # Missing "text" key+ {"type": "output_text", "text": "valid output", "id": "msg_123"},+ {"type": "output_text", "id": "msg_123"}, # Missing "text" key+ ]+ )+ ]+ result = _construct_responses_api_input(messages)++ # Should only include blocks with valid text content+ assert len(result) == 1+ assert result[0]["type"] == "message"+ assert result[0]["role"] == "assistant"+ assert len(result[0]["content"]) == 2+ assert result[0]["content"][0] == {+ "type": "output_text",+ "text": "valid text",+ "annotations": [],+ }+ assert result[0]["content"][1] == {+ "type": "output_text",+ "text": "valid output",+ "annotations": [],+ }++def test__construct_responses_api_input_human_message_with_image_url_conversion() -> (None):