The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix(openai): preserve reasoning item boundaries (#39278)
Alex Onufrak32c15bbbd553
diff --git a/libs/partners/openai/langchain_openai/chat_models/_compat.py b/libs/partners/openai/langchain_openai/chat_models/_compat.pyindex d64c854ad7..d46971b447 100644--- a/libs/partners/openai/langchain_openai/chat_models/_compat.py+++ b/libs/partners/openai/langchain_openai/chat_models/_compat.py@@ -212,6 +212,15 @@ def _convert_annotation_from_v1(annotation: types.Annotation) -> dict[str, Any]:return dict(annotation)+def _same_reasoning_item(first: dict[str, Any], second: dict[str, Any]) -> bool:+ """Return whether two reasoning fragments share an item identity."""+ first_has_id = "id" in first+ second_has_id = "id" in second+ return first_has_id == second_has_id and (+ not first_has_id or first["id"] == second["id"]+ )++def _implode_reasoning_blocks(blocks: list[dict[str, Any]]) -> Iterable[dict[str, Any]]:i = 0n = len(blocks)@@ -249,7 +258,11 @@ def _implode_reasoning_blocks(blocks: list[dict[str, Any]]) -> Iterable[dict[stri += 1while i < n:next_ = blocks[i]- if next_.get("type") == "reasoning" and "reasoning" in next_:+ if (+ next_.get("type") == "reasoning"+ and "reasoning" in next_+ and _same_reasoning_item(block, next_)+ ):summary.append({"type": "summary_text", "text": next_.get("reasoning", "")})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 afa5515447..4e3fc62d1b 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@@ -4007,6 +4007,86 @@ def test_convert_from_v1_to_responses(assert message_v1 != result+def test_convert_from_v1_to_responses_preserves_reasoning_item_boundaries() -> None:+ content: list[types.ContentBlock] = [+ {+ "type": "reasoning",+ "id": "rs_123",+ "reasoning": "first ",+ "extras": {+ "encrypted_content": "encrypted-123",+ "status": "completed",+ },+ },+ {+ "type": "reasoning",+ "id": "rs_123",+ "reasoning": "second",+ },+ {+ "type": "reasoning",+ "id": "rs_456",+ "reasoning": "third",+ "extras": {+ "encrypted_content": "encrypted-456",+ "status": "incomplete",+ },+ },+ {"type": "reasoning", "reasoning": "legacy "},+ {"type": "reasoning", "reasoning": "reasoning"},+ {"type": "reasoning", "id": "rs_789", "reasoning": "last"},+ cast(+ types.ContentBlock,+ {+ "type": "reasoning",+ "id": "rs_native",+ "summary": [{"type": "summary_text", "text": "already native"}],+ "encrypted_content": "encrypted-native",+ },+ ),+ ]++ result = _convert_from_v1_to_responses(content, [])++ assert result == [+ {+ "type": "reasoning",+ "id": "rs_123",+ "summary": [+ {"type": "summary_text", "text": "first "},+ {"type": "summary_text", "text": "second"},+ ],+ "encrypted_content": "encrypted-123",+ "status": "completed",+ },+ {+ "type": "reasoning",+ "id": "rs_456",+ "summary": [{"type": "summary_text", "text": "third"}],+ "encrypted_content": "encrypted-456",+ "status": "incomplete",+ },+ {+ "type": "reasoning",+ "summary": [+ {"type": "summary_text", "text": "legacy "},+ {"type": "summary_text", "text": "reasoning"},+ ],+ },+ {+ "type": "reasoning",+ "id": "rs_789",+ "summary": [{"type": "summary_text", "text": "last"}],+ },+ {+ "type": "reasoning",+ "id": "rs_native",+ "summary": [{"type": "summary_text", "text": "already native"}],+ "encrypted_content": "encrypted-native",+ },+ ]++def test_convert_from_v1_to_responses_missing_type() -> None:"""Regression: blocks without 'type' should be skipped, not raise KeyError."""content: list = [