The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): normalize list-shaped streaming content deltas
Jakub Wolniewicz23bae43cfad6
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex b6c46c85d42..033699ebbe0 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -4206,13 +4206,16 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=_fire_first_delta()agent._fire_reasoning_delta(reasoning_text)- # Accumulate text content — fire callback only when no tool calls- delta_content = getattr(delta, "content", None)+ # Accumulate text content — fire callback only when no tool calls.+ # Some OpenAI-compatible providers emit a text delta as a list of+ # content blocks. Convert it once so callbacks and the synthetic+ # completion message always receive plain text.+ delta_content = flatten_message_text(getattr(delta, "content", None), sep="")if delta_content:content_parts.append(delta_content)if not tool_calls_acc:- if pending_text_parts or _provider_stream_text_may_be_sse(delta.content):- pending_text_parts.append(delta.content)+ if pending_text_parts or _provider_stream_text_may_be_sse(delta_content):+ pending_text_parts.append(delta_content)pending_text = "".join(pending_text_parts)if _provider_stream_text_may_be_sse(pending_text):continuediff --git a/tests/run_agent/test_streaming.py b/tests/run_agent/test_streaming.pyindex da6014320d9..e5b0471e64b 100644--- a/tests/run_agent/test_streaming.py+++ b/tests/run_agent/test_streaming.py@@ -354,6 +354,49 @@ class TestStreamingCallbacks:+ @patch("run_agent.AIAgent._create_request_openai_client")+ @patch("run_agent.AIAgent._close_request_openai_client")+ def test_list_content_after_tool_call_is_normalized(self, mock_close, mock_create):+ """OpenAI-compatible content blocks must never reach stream callbacks raw.++ Mistral/NVIDIA can emit a text delta as a list of content-block dicts.+ The list must be flattened before the first direct stream callback and+ again after a tool-call has switched the stream to the suppression+ callback path.+ """+ from run_agent import AIAgent++ chunks = [+ _make_stream_chunk(content=[{"type": "text", "text": "before tool; "}]),+ _make_stream_chunk(tool_calls=[+ _make_tool_call_delta(index=0, tc_id="call_63734", name="read_file")+ ]),+ _make_stream_chunk(content=[{"type": "text", "text": "after tool"}]),+ _make_stream_chunk(finish_reason="tool_calls"),+ ]+ deltas = []++ mock_client = MagicMock()+ mock_client.chat.completions.create.return_value = iter(chunks)+ mock_create.return_value = mock_client++ agent = AIAgent(+ api_key="test-key",+ base_url="https://openrouter.ai/api/v1",+ model="test/model",+ quiet_mode=True,+ skip_context_files=True,+ skip_memory=True,+ stream_delta_callback=deltas.append,+ )+ agent.api_mode = "chat_completions"+ agent._interrupt_requested = False++ response = agent._interruptible_streaming_api_call({})++ assert deltas == ["before tool; ", "after tool"]+ assert response.choices[0].message.content == "before tool; after tool"+# ── Test: Streaming Fallback ────────────────────────────────────────────