Skip to content

The agent engineering platform.

fix(openai): preserve streamed encrypted reasoning (#39635)

Johannes du Plessis7b954aa9efa9
diff --git a/libs/partners/openai/langchain_openai/chat_models/base.py b/libs/partners/openai/langchain_openai/chat_models/base.py
index 16baf11bf8..03d9eb4b4a 100644
--- a/libs/partners/openai/langchain_openai/chat_models/base.py
+++ b/libs/partners/openai/langchain_openai/chat_models/base.py
@@ -4706,7 +4706,7 @@ def _construct_responses_api_input(
new_item["phase"] = phase
input_.append(new_item)
elif block_type == "reasoning":
- if store is not False or "encrypted_content" in block:
+ if store is not False or block.get("encrypted_content"):
input_.append(_pop_index_and_sub_index(block))
elif block_type in (
"compaction",
@@ -5239,8 +5239,27 @@ def _convert_responses_chunk_to_generation_chunk(
_advance(chunk.output_index)
current_sub_index = 0
reasoning = chunk.item.model_dump(exclude_none=True, mode="json")
+ # Encrypted content is only complete on the corresponding `done` event, which
+ # emits it below. Drop it here so the two events don't merge into a doubled
+ # string (blocks sharing an `index` are merged by concatenation).
+ reasoning.pop("encrypted_content", None)
reasoning["index"] = current_index
content.append(reasoning)
+ elif (
+ chunk.type == "response.output_item.done"
+ and chunk.item.type == "reasoning"
+ and chunk.item.encrypted_content
+ ):
+ _advance(chunk.output_index)
+ content.append(
+ {
+ "type": "reasoning",
+ "id": chunk.item.id,
+ "summary": [],
+ "encrypted_content": chunk.item.encrypted_content,
+ "index": current_index,
+ }
+ )
elif chunk.type == "response.reasoning_summary_part.added":
_advance(chunk.output_index)
content.append(
diff --git a/libs/partners/openai/tests/cassettes/test_mcp_builtin_zdr.yaml.gz b/libs/partners/openai/tests/cassettes/test_mcp_builtin_zdr.yaml.gz
index 310a04d31c..1d8d1235a2 100644
Binary files a/libs/partners/openai/tests/cassettes/test_mcp_builtin_zdr.yaml.gz and b/libs/partners/openai/tests/cassettes/test_mcp_builtin_zdr.yaml.gz differ
diff --git a/libs/partners/openai/tests/cassettes/test_stream_encrypted_reasoning.yaml.gz b/libs/partners/openai/tests/cassettes/test_stream_encrypted_reasoning.yaml.gz
new file mode 100644
index 0000000000..030a5eec97
Binary files /dev/null and b/libs/partners/openai/tests/cassettes/test_stream_encrypted_reasoning.yaml.gz differ
diff --git a/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py b/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py
index b17b0ba6db..f5d3ae7205 100644
--- a/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py
+++ b/libs/partners/openai/tests/integration_tests/chat_models/test_responses_api.py
@@ -782,6 +782,35 @@ def test_stream_reasoning_summary(
assert isinstance(response_2, AIMessage)
+def test_stream_encrypted_reasoning() -> None:
+ llm = ChatOpenAI(
+ model="gpt-5.6-luna",
+ use_responses_api=True,
+ reasoning_effort="medium",
+ store=False,
+ include=["reasoning.encrypted_content"],
+ )
+ message_1 = {
+ "role": "user",
+ "content": "What was the third tallest buliding in the year 2000?",
+ }
+ response_1 = llm.stream_events([message_1], version="v3").output
+ total_reasoning_blocks = 0
+ for block in response_1.content_blocks:
+ if block["type"] == "reasoning":
+ total_reasoning_blocks += 1
+ assert isinstance(block.get("id"), str)
+ assert block.get("id", "").startswith("rs_")
+ assert isinstance(block["extras"].get("encrypted_content"), str)
+ assert isinstance(block.get("index"), str)
+
+ # Check we can pass back summaries
+ message_2 = {"role": "user", "content": "Thank you."}
+ response_2 = llm.invoke([message_1, response_1, message_2])
+ assert isinstance(response_2, AIMessage)
+
+
@pytest.mark.default_cassette("test_code_interpreter.yaml.gz")
@pytest.mark.vcr
@pytest.mark.parametrize(
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.py
index 3cc4474ccc..887e31d739 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
@@ -3052,12 +3052,14 @@ def test__construct_responses_api_input_human_message_with_image_url_conversion(
def test__construct_responses_api_input_store_false_replays_stateless_history() -> None:
ai_message = AIMessage(
content=[
+ # Dropped: no encrypted content to replay, in both shapes it can take.
{"type": "reasoning", "id": "rs_123", "summary": []},
+ {"type": "reasoning", "encrypted_content": ""},
{
"type": "reasoning",
- "id": "rs_456",
+ "id": "rs_789",
"summary": [],
- "encrypted_content": "",
+ "encrypted_content": "encrypted-reasoning",
},
{"type": "text", "text": "Use pathlib.rglob.", "id": "msg_123"},
],
@@ -3069,9 +3071,9 @@ def test__construct_responses_api_input_store_false_replays_stateless_history()
assert result == [
{
"type": "reasoning",
- "id": "rs_456",
+ "id": "rs_789",
"summary": [],
- "encrypted_content": "",
+ "encrypted_content": "encrypted-reasoning",
},
{
"type": "message",
diff --git a/libs/partners/openai/tests/unit_tests/chat_models/test_responses_stream.py b/libs/partners/openai/tests/unit_tests/chat_models/test_responses_stream.py
index ba2f786621..7b0b47b8f8 100644
--- a/libs/partners/openai/tests/unit_tests/chat_models/test_responses_stream.py
+++ b/libs/partners/openai/tests/unit_tests/chat_models/test_responses_stream.py
@@ -47,6 +47,9 @@ from openai.types.shared.reasoning import Reasoning
from openai.types.shared.response_format_text import ResponseFormatText
from langchain_openai import ChatOpenAI
+from langchain_openai.chat_models.base import (
+ _convert_responses_chunk_to_generation_chunk,
+)
from tests.unit_tests.chat_models.test_base import MockSyncContextManager
MODEL = "gpt-5.4"
@@ -349,6 +352,8 @@ responses_stream = [
id="rs_234",
summary=[],
type="reasoning",
+ # Deliberately populated: pins that the `added` event's encrypted content
+ # is dropped rather than merged with the `done` event's copy below.
encrypted_content="encrypted-content",
status=None,
),
@@ -766,6 +771,74 @@ def test_responses_stream(output_version: str, expected_content: list[dict]) ->
assert dumped == payload["input"][idx]
[email protected]("output_version", ["responses/v1", "v1"])
+def test_responses_stream_encrypted_reasoning_replays_with_store_false(
+ output_version: str,
+) -> None:
+ """Streamed encrypted reasoning survives a stateless (`store=False`) replay.
+
+ Regression test for the round trip users actually hit: with `store=False`,
+ reasoning can only be replayed via its encrypted content, so a reasoning item
+ that carried one must come back with it intact -- exactly once -- while an item
+ that carried none is dropped rather than replayed as an unresolvable item ID.
+ """
+ llm = ChatOpenAI(
+ model=MODEL, use_responses_api=True, output_version=output_version, store=False
+ )
+ mock_client = MagicMock()
+
+ def mock_create(*args: Any, **kwargs: Any) -> MockSyncContextManager:
+ return MockSyncContextManager(responses_stream)
+
+ mock_client.responses.create = mock_create
+
+ full: BaseMessageChunk | None = None
+ with patch.object(llm, "root_client", mock_client):
+ for chunk in llm.stream("test"):
+ full = chunk if full is None else full + chunk
+ assert isinstance(full, AIMessageChunk)
+
+ payload = llm._get_request_payload([full], store=False)
+ reasoning_items = [
+ item for item in payload["input"] if item.get("type") == "reasoning"
+ ]
+
+ # `rs_123` carried no encrypted content and is dropped; `rs_234` is replayed.
+ assert [item["id"] for item in reasoning_items] == ["rs_234"]
+ assert reasoning_items[0]["encrypted_content"] == "encrypted-content"
+
+
+def test_responses_reasoning_done_without_encrypted_content_emits_no_chunk() -> None:
+ """A reasoning `done` event with no encrypted content yields no chunk at all.
+
+ The event carries nothing the `added` event has not already surfaced, so
+ emitting a chunk for it would mean an extra empty `on_llm_new_token` callback
+ for every reasoning item -- the common case, since encrypted content is only
+ populated when the caller opts into it.
+ """
+ for encrypted_content in (None, ""):
+ event = ResponseOutputItemDoneEvent(
+ item=ResponseReasoningItem(
+ id="rs_123",
+ summary=[Summary(text="reasoning", type="summary_text")],
+ type="reasoning",
+ encrypted_content=encrypted_content,
+ status=None,
+ ),
+ output_index=0,
+ sequence_number=1,
+ type="response.output_item.done",
+ )
+
+ _, _, _, generation_chunk = _convert_responses_chunk_to_generation_chunk(
+ event, 0, 0, 0
+ )
+
+ assert generation_chunk is None, (
+ f"expected no chunk for encrypted_content={encrypted_content!r}"
+ )
+
+
def test_responses_stream_events_v3_emits_reasoning_lifecycle() -> None:
"""v3 streaming emits `content-block-finish` events for reasoning blocks.