Skip to content

The agent engineering platform.

fix(core): propagate extras on content-block-start events (#41272)

ccurmeb7b942a7ffd3
diff --git a/libs/core/langchain_core/language_models/_compat_bridge.py b/libs/core/langchain_core/language_models/_compat_bridge.py
index 81f592f043..11a6fcbe6e 100644
--- a/libs/core/langchain_core/language_models/_compat_bridge.py
+++ b/libs/core/langchain_core/language_models/_compat_bridge.py
@@ -47,10 +47,8 @@ from langchain_protocol.protocol import (
MessageMetadata,
MessagesData,
MessageStartData,
- ReasoningContentBlock,
ServerToolCall,
ServerToolCallChunk,
- TextContentBlock,
ToolCall,
ToolCallChunk,
UsageInfo,
@@ -242,13 +240,21 @@ def _start_skeleton(block: CompatBlock) -> ContentBlock:
audio, video, file, non_standard, finalized tool calls) drop their
heavy payload fields; those are carried by `content-block-finish`.
Correlation fields (id, name, toolCallId) and small metadata
- (mime_type, url, status, …) are preserved on the start event.
+ (mime_type, url, status, phase, …) are preserved on the start event.
"""
btype = block.get("type", "text")
if btype == "text":
- return TextContentBlock(type="text", text="")
+ # extras and id
+ text_skel = {
+ k: v for k, v in block.items() if k not in {"text", "index", "annotations"}
+ }
+ text_skel["text"] = ""
+ return _to_protocol_block(text_skel)
if btype == "reasoning":
- return ReasoningContentBlock(type="reasoning", reasoning="")
+ # extras and id
+ r_skel = {k: v for k, v in block.items() if k not in {"reasoning", "index"}}
+ r_skel["reasoning"] = ""
+ return _to_protocol_block(r_skel)
if btype == "tool_call_chunk":
return ToolCallChunk(
type="tool_call_chunk",
diff --git a/libs/core/tests/unit_tests/language_models/test_compat_bridge.py b/libs/core/tests/unit_tests/language_models/test_compat_bridge.py
index 0030e2c381..2332ab39f6 100644
--- a/libs/core/tests/unit_tests/language_models/test_compat_bridge.py
+++ b/libs/core/tests/unit_tests/language_models/test_compat_bridge.py
@@ -1061,6 +1061,43 @@ def test_message_to_events_self_contained_start_strips_heavy_fields() -> None:
assert finishes[2]["content"]["value"] == {"big": "C" * 1024}
+def test_chunks_to_events_text_start_preserves_metadata() -> None:
+ """Text/reasoning start keeps small metadata (e.g. `phase`) but no payload."""
+ blocks: list[dict[str, Any]] = [
+ {"type": "reasoning", "reasoning": "hmm", "id": "rs_1", "index": 0},
+ {"type": "text", "text": "", "phase": "commentary", "index": 1},
+ {
+ "type": "text",
+ "text": "Hi",
+ "id": "msg_1",
+ "annotations": [{"type": "citation", "url": "u"}],
+ "index": 1,
+ },
+ ]
+ chunks = [
+ ChatGenerationChunk(message=AIMessageChunk(content=[block], id="msg-1"))
+ for block in blocks
+ ]
+ events = list(chunks_to_events(iter(chunks)))
+ assert_valid_event_stream(events)
+
+ starts: list[Any] = [e for e in events if e["event"] == "content-block-start"]
+ assert starts[0]["content"] == {
+ "type": "reasoning",
+ "reasoning": "",
+ "id": "rs_1",
+ }
+ assert starts[1]["content"] == {
+ "type": "text",
+ "text": "",
+ "phase": "commentary",
+ }
+
+ finishes: list[Any] = [e for e in events if e["event"] == "content-block-finish"]
+ assert finishes[1]["content"]["phase"] == "commentary"
+ assert finishes[1]["content"]["text"] == "Hi"
+
+
def test_message_to_events_finalized_tool_call_start_strips_args() -> None:
"""Finalized `tool_call` keeps id/name on start but not parsed args."""
msg = AIMessage(