Use Claude Code, Codex, VSCode, Pi, and OpenCode (and 6 other harnesses) for free (1.3B+ free tokens) from your terminal, app, IDE, or phone, and now from the browser with native browser sessions (multi-harness + multi-model) like OpenClaw (voice supported + ToS friendly)
Python
fix(206): defer post-tool assistant content for OpenAI chat conversion
Alishahryar136d236b563c0
diff --git a/core/anthropic/conversion.py b/core/anthropic/conversion.pyindex 94eba435..84ba6140 100644--- a/core/anthropic/conversion.py+++ b/core/anthropic/conversion.py@@ -1,6 +1,7 @@"""Message and tool format converters."""import json+from dataclasses import dataclass, fieldfrom enum import StrEnumfrom typing import Any@@ -82,6 +83,81 @@ def _think_tag_content(reasoning: str) -> str:return f"<think>\n{reasoning}\n</think>"+@dataclass+class _PendingAfterTools:+ """Assistant content that appears after ``tool_use`` in an Anthropic message.++ OpenAI ``chat.completions`` cannot place assistant text after ``tool_calls`` in the+ same message, so it is deferred until the corresponding ``role: tool`` results have+ been replayed in order.+ """++ # Tool use IDs still missing a ``role: tool`` result before post-tool text may be replayed.+ remaining_tool_ids: set[str] = field(default_factory=set)+ deferred_blocks: list[Any] = field(default_factory=list)+ top_level_reasoning: str | None = None+ reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.THINK_TAGS+ # True after deferred assistant text has been added to the OpenAI transcript.+ deferred_emitted: bool = False++ def needs_deferred(self) -> bool:+ return bool(self.deferred_blocks) and not self.deferred_emitted+++def _index_first_tool_use(blocks: list[Any]) -> int | None:+ for i, block in enumerate(blocks):+ if get_block_type(block) == "tool_use":+ return i+ return None+++def _iter_tool_uses_in_order(blocks: list[Any]) -> list[dict[str, Any]]:+ tool_calls: list[dict[str, Any]] = []+ for block in blocks:+ if get_block_type(block) == "tool_use":+ tool_input = get_block_attr(block, "input", {})+ tool_calls.append(+ {+ "id": get_block_attr(block, "id"),+ "type": "function",+ "function": {+ "name": get_block_attr(block, "name"),+ "arguments": json.dumps(tool_input)+ if isinstance(tool_input, dict)+ else str(tool_input),+ },+ }+ )+ return tool_calls+++def _deferred_post_tool_blocks(+ content: list[Any], *, first_tool_index: int+) -> list[Any]:+ return [+ b+ for i, b in enumerate(content)+ if i > first_tool_index and get_block_type(b) != "tool_use"+ ]+++def _assert_no_forbidden_assistant_block(block: Any) -> None:+ block_type = get_block_type(block)+ if block_type == "image":+ raise OpenAIConversionError(+ "Assistant image blocks are not supported for OpenAI chat conversion."+ )+ if block_type in (+ "server_tool_use",+ "web_search_tool_result",+ "web_fetch_tool_result",+ ):+ raise OpenAIConversionError(+ "OpenAI chat conversion does not support Anthropic server tool blocks "+ f"({block_type!r} in an assistant message). Use a native Anthropic transport provider."+ )++class AnthropicToOpenAIConverter:"""Convert Anthropic message format to OpenAI-compatible format."""@@ -91,7 +167,8 @@ class AnthropicToOpenAIConverter:*,reasoning_replay: ReasoningReplayMode = ReasoningReplayMode.THINK_TAGS,) -> list[dict[str, Any]]:- result = []+ result: list[dict[str, Any]] = []+ pending: _PendingAfterTools | None = Nonefor msg in messages:role = msg.role@@ -100,7 +177,52 @@ class AnthropicToOpenAIConverter:getattr(msg, "reasoning_content", None))- if isinstance(content, str):+ if role == "assistant" and isinstance(content, list):+ if pending is not None and pending.needs_deferred():+ # Orphan: expected tool result; emit deferred to avoid a stuck session.+ result.extend(+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(+ pending,+ )+ )+ pending.deferred_emitted = True+ pending = None++ if (first_i := _index_first_tool_use(content)) is not None:+ for block in content:+ if get_block_type(block) == "tool_use":+ continue+ _assert_no_forbidden_assistant_block(block)+ out, new_pending = (+ AnthropicToOpenAIConverter._convert_assistant_message_with_split(+ content,+ first_tool_index=first_i,+ reasoning_content=reasoning_content,+ reasoning_replay=reasoning_replay,+ )+ )+ result.extend(out)+ if new_pending is not None:+ pending = new_pending+ else:+ for block in content:+ _assert_no_forbidden_assistant_block(block)+ result.extend(+ AnthropicToOpenAIConverter._convert_assistant_message(+ content,+ reasoning_content=reasoning_content,+ reasoning_replay=reasoning_replay,+ )+ )+ elif isinstance(content, str):+ if role == "user" and pending is not None and pending.needs_deferred():+ result.extend(+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(+ pending+ )+ )+ pending.deferred_emitted = True+ pending = Noneconverted = {"role": role, "content": content}if role == "assistant" and reasoning_content:if reasoning_replay == ReasoningReplayMode.REASONING_CONTENT:@@ -112,23 +234,107 @@ class AnthropicToOpenAIConverter:converted["content"] = "\n\n".join(content_parts)result.append(converted)elif isinstance(content, list):- if role == "assistant":- result.extend(- AnthropicToOpenAIConverter._convert_assistant_message(- content,- reasoning_content=reasoning_content,- reasoning_replay=reasoning_replay,+ if role == "user":+ if pending is not None and pending.needs_deferred():+ if not pending.remaining_tool_ids:+ result.extend(+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(+ pending+ )+ )+ pending.deferred_emitted = True+ pending = None+ result.extend(+ AnthropicToOpenAIConverter._convert_user_message(+ content+ )+ )+ else:+ pieces = AnthropicToOpenAIConverter._convert_user_message_with_injection(+ content, pending+ )+ result.extend(pieces["messages"])+ if pieces["cleared_pending"]:+ pending = None+ else:+ result.extend(+ AnthropicToOpenAIConverter._convert_user_message(content))- )- elif role == "user":+ else:+ if role == "user" and pending is not None and pending.needs_deferred():result.extend(- AnthropicToOpenAIConverter._convert_user_message(content)+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(+ pending+ ))- else:+ pending.deferred_emitted = True+ pending = Noneresult.append({"role": role, "content": str(content)})+ if pending is not None and pending.needs_deferred():+ result.extend(+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(pending)+ )+return result+ @staticmethod+ def _convert_assistant_message_with_split(+ content: list[Any],+ *,+ first_tool_index: int,+ reasoning_content: str | None,+ reasoning_replay: ReasoningReplayMode,+ ) -> tuple[list[dict[str, Any]], _PendingAfterTools | None]:+ pre = content[:first_tool_index]+ tool_calls = _iter_tool_uses_in_order(content)+ if not tool_calls:+ return (+ AnthropicToOpenAIConverter._convert_assistant_message(+ content,+ reasoning_content=reasoning_content,+ reasoning_replay=reasoning_replay,+ ),+ None,+ )+ deferred_blocks = _deferred_post_tool_blocks(+ content, first_tool_index=first_tool_index+ )++ pre_msg: dict[str, Any]+ if not pre:+ pre_msg = {+ "role": "assistant",+ "content": "",+ }+ if reasoning_replay == ReasoningReplayMode.REASONING_CONTENT:+ replay = reasoning_content+ if replay:+ pre_msg["reasoning_content"] = replay+ else:+ pre_msg = AnthropicToOpenAIConverter._convert_assistant_message(+ pre,+ reasoning_content=reasoning_content,+ reasoning_replay=reasoning_replay,+ )[0]+ pre_msg["tool_calls"] = tool_calls+ if tool_calls and pre_msg.get("content") == " ":+ pre_msg["content"] = ""+ pnd: _PendingAfterTools | None = None+ if deferred_blocks:+ res_ids: set[str] = set()+ for tc in tool_calls:+ tid = tc.get("id")+ if tid is not None and str(tid).strip() != "":+ res_ids.add(str(tid))+ pnd = _PendingAfterTools(+ remaining_tool_ids=res_ids,+ deferred_blocks=deferred_blocks,+ top_level_reasoning=reasoning_content,+ reasoning_replay=reasoning_replay,+ )+ return [pre_msg], pnd+@staticmethoddef _convert_assistant_message(content: list[Any],@@ -139,28 +345,13 @@ class AnthropicToOpenAIConverter:content_parts: list[str] = []thinking_parts: list[str] = []tool_calls: list[dict[str, Any]] = []- seen_tool_use = False-for block in content:block_type = get_block_type(block)-if block_type == "text":- if seen_tool_use:- raise OpenAIConversionError(- "OpenAI chat conversion does not support assistant text after "- "tool_use in the same message; split the transcript or use a "- "native Anthropic provider."- )content_parts.append(get_block_attr(block, "text", ""))elif block_type == "thinking":if reasoning_replay == ReasoningReplayMode.DISABLED:continue- if seen_tool_use:- raise OpenAIConversionError(- "OpenAI chat conversion does not support assistant thinking after "- "tool_use in the same message; split the transcript or use a "- "native Anthropic provider."- )thinking = get_block_attr(block, "thinking", "")if reasoning_replay == ReasoningReplayMode.THINK_TAGS:content_parts.append(_think_tag_content(thinking))@@ -171,7 +362,6 @@ class AnthropicToOpenAIConverter:# or reasoning_content for OpenAI chat upstreams.continueelif block_type == "tool_use":- seen_tool_use = Truetool_input = get_block_attr(block, "input", {})tool_calls.append({@@ -185,19 +375,8 @@ class AnthropicToOpenAIConverter:},})- elif block_type == "image":- raise OpenAIConversionError(- "Assistant image blocks are not supported for OpenAI chat conversion."- )- elif block_type in (- "server_tool_use",- "web_search_tool_result",- "web_fetch_tool_result",- ):- raise OpenAIConversionError(- "OpenAI chat conversion does not support Anthropic server tool blocks "- f"({block_type!r} in an assistant message). Use a native Anthropic transport provider."- )+ else:+ _assert_no_forbidden_assistant_block(block)content_str = "\n\n".join(content_parts)if not content_str and not tool_calls:@@ -216,6 +395,77 @@ class AnthropicToOpenAIConverter:return [msg]+ @staticmethod+ def _deferred_post_tool_to_messages(+ pending: _PendingAfterTools,+ ) -> list[dict[str, Any]]:+ if not pending.deferred_blocks:+ return []+ return AnthropicToOpenAIConverter._convert_assistant_message(+ pending.deferred_blocks,+ reasoning_content=pending.top_level_reasoning,+ reasoning_replay=pending.reasoning_replay,+ )++ @staticmethod+ def _convert_user_message_with_injection(+ content: list[Any], pending: _PendingAfterTools+ ) -> dict[str, Any]:+ """Convert user list blocks, emitting deferred assistant after all tool results."""+ if not pending.needs_deferred() or not pending.remaining_tool_ids:+ return {+ "messages": AnthropicToOpenAIConverter._convert_user_message(content),+ "cleared_pending": False,+ }++ result: list[dict[str, Any]] = []+ text_parts: list[str] = []+ cleared = False++ def flush_text() -> None:+ if text_parts:+ result.append({"role": "user", "content": "\n".join(text_parts)})+ text_parts.clear()++ for block in content:+ block_type = get_block_type(block)+ if block_type == "text":+ text_parts.append(get_block_attr(block, "text", ""))+ elif block_type == "image":+ raise OpenAIConversionError(+ "User message image blocks are not supported for OpenAI chat "+ "conversion; use a vision-capable native Anthropic provider or "+ "extend the converter."+ )+ elif block_type == "tool_result":+ flush_text()+ tool_content = get_block_attr(block, "content", "")+ serialized = _serialize_tool_result_content(tool_content)+ tuid = get_block_attr(block, "tool_use_id")+ tuid_s = str(tuid) if tuid is not None else ""+ result.append(+ {+ "role": "tool",+ "tool_call_id": tuid,+ "content": serialized if serialized else "",+ }+ )+ if tuid_s in pending.remaining_tool_ids:+ pending.remaining_tool_ids.discard(tuid_s)+ if not pending.remaining_tool_ids:+ result.extend(+ AnthropicToOpenAIConverter._deferred_post_tool_to_messages(+ pending+ )+ )+ pending.deferred_emitted = True+ cleared = True+ else:+ pass++ flush_text()+ return {"messages": result, "cleared_pending": cleared}+@staticmethoddef _convert_user_message(content: list[Any]) -> list[dict[str, Any]]:result: list[dict[str, Any]] = []diff --git a/core/anthropic/sse.py b/core/anthropic/sse.pyindex 3bf5ebe8..3923f0f4 100644--- a/core/anthropic/sse.py+++ b/core/anthropic/sse.py@@ -120,6 +120,10 @@ class ContentBlockManager:state.task_arg_buffer = ""return args_json+ def has_emitted_tool_block(self) -> bool:+ """True when native OpenAI tool streaming has started a ``tool_use`` block."""+ return any(s.started for s in self.tool_states.values())+def flush_task_arg_buffers(self) -> list[tuple[int, str]]:results: list[tuple[int, str]] = []for tool_index, state in list(self.tool_states.items()):@@ -353,6 +357,19 @@ class SSEBuilder:yield self.content_block_delta(error_index, "text_delta", error_message)yield self.content_block_stop(error_index)+ def emit_top_level_error(self, error_message: str) -> str:+ """Emit a top-level ``event: error`` (not assistant text) for transport failures."""+ return self._format_event(+ "error",+ {+ "type": "error",+ "error": {+ "type": "api_error",+ "message": error_message,+ },+ },+ )+@propertydef accumulated_text(self) -> str:return "".join(self._accumulated_text_parts)diff --git a/providers/openai_compat.py b/providers/openai_compat.pyindex 1c8469cc..88ff24be 100644--- a/providers/openai_compat.py+++ b/providers/openai_compat.py@@ -351,8 +351,13 @@ class OpenAIChatTransport(BaseProvider):)for event in sse.close_all_blocks():yield event- for event in sse.emit_error(error_message):- yield event+ if sse.blocks.has_emitted_tool_block():+ # Avoid a second assistant text block after an emitted tool_use, which+ # breaks OpenAI history replay (issue #206) when Claude Code stores it.+ yield sse.emit_top_level_error(error_message)+ else:+ for event in sse.emit_error(error_message):+ yield eventyield sse.message_delta("end_turn", 1)yield sse.message_stop()returndiff --git a/smoke/product/test_provider_product_live.py b/smoke/product/test_provider_product_live.pyindex 469163bf..783bee21 100644--- a/smoke/product/test_provider_product_live.py+++ b/smoke/product/test_provider_product_live.py@@ -52,6 +52,12 @@ def test_provider_interleaved_thinking_tool_e2e(smoke_config: SmokeConfig) -> No_run_for_each_provider(smoke_config, _scenario_interleaved_history)[email protected]_target("tools")+def test_provider_tool_use_then_text_history_e2e(smoke_config: SmokeConfig) -> None:+ """OpenAI-compatible path: history with tool_use + assistant text after tool (issue #206)."""+ _run_for_each_provider(smoke_config, _scenario_tool_use_then_text_in_history)++@pytest.mark.smoke_target("tools")def test_provider_tool_result_continuation_e2e(smoke_config: SmokeConfig) -> None:_run_for_each_provider(smoke_config, _scenario_tool_result_continuation)@@ -249,6 +255,52 @@ def _scenario_interleaved_history(assert_product_stream(turn.events)+def _scenario_tool_use_then_text_in_history(+ smoke_config: SmokeConfig, provider_model: ProviderModel+) -> None:+ tool_id = "toolu_206_smoke"+ payload = {+ "model": "claude-sonnet-4-5-20250929",+ "max_tokens": 256,+ "messages": [+ {"role": "user", "content": "We will use echo_smoke once in this session."},+ {+ "role": "assistant",+ "content": [+ {+ "type": "tool_use",+ "id": tool_id,+ "name": "echo_smoke",+ "input": {"value": "FCC_206_SMOKE"},+ },+ {+ "type": "text",+ "text": "Narration after the tool call (issue #206 shape).",+ },+ ],+ },+ {+ "role": "user",+ "content": [+ {+ "type": "tool_result",+ "tool_use_id": tool_id,+ "content": "FCC_206_SMOKE",+ },+ ],+ },+ {+ "role": "user",+ "content": "Reply in one short sentence: did you see the echo value?",+ },+ ],+ "tools": [echo_tool_schema()],+ }+ with _server_for_provider(smoke_config, provider_model, "tool-206") as server:+ turn = ConversationDriver(server, smoke_config).stream(payload)+ assert_product_stream(turn.events)++def _scenario_tool_result_continuation(smoke_config: SmokeConfig, provider_model: ProviderModel) -> None:diff --git a/tests/providers/test_converter.py b/tests/providers/test_converter.pyindex a5aa6b16..a0b7c1ab 100644--- a/tests/providers/test_converter.py+++ b/tests/providers/test_converter.py@@ -573,14 +573,44 @@ def test_convert_user_message_image_raises():AnthropicToOpenAIConverter.convert_messages(messages)-def test_convert_assistant_text_after_tool_use_raises():+def test_convert_assistant_text_after_tool_use_splits_for_openai_chat():+ """Post-tool_use assistant text is replayed as a second assistant turn (issue 206)."""content = [MockBlock(type="tool_use", id="call_z", name="Read", input={}),- MockBlock(type="text", text="Illegal after tool"),+ MockBlock(type="text", text="After tool"),]messages = [MockMessage("assistant", content)]- with pytest.raises(OpenAIConversionError):- AnthropicToOpenAIConverter.convert_messages(messages)+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert len(result) == 2+ assert result[0]["role"] == "assistant"+ assert result[0]["tool_calls"][0]["id"] == "call_z"+ assert result[1] == {"role": "assistant", "content": "After tool"}+++def test_convert_assistant_text_after_tool_use_inserts_after_tool_results():+ messages = [+ MockMessage(+ "assistant",+ [+ MockBlock(type="tool_use", id="call_z", name="Read", input={}),+ MockBlock(type="text", text="Post-tool commentary"),+ ],+ ),+ MockMessage(+ "user",+ [+ MockBlock(+ type="tool_result",+ tool_use_id="call_z",+ content="file contents",+ )+ ],+ ),+ ]+ result = AnthropicToOpenAIConverter.convert_messages(messages)+ assert result[0]["role"] == "assistant" and "tool_calls" in result[0]+ assert result[1]["role"] == "tool" and result[1]["tool_call_id"] == "call_z"+ assert result[2] == {"role": "assistant", "content": "Post-tool commentary"}def test_openai_build_accepts_declared_native_top_level_hints() -> None:diff --git a/tests/providers/test_nvidia_nim.py b/tests/providers/test_nvidia_nim.pyindex f9a9095d..43142073 100644--- a/tests/providers/test_nvidia_nim.py+++ b/tests/providers/test_nvidia_nim.py@@ -153,6 +153,44 @@ async def test_build_request_body_omits_reasoning_when_request_disables_thinkingassert "reasoning_budget" not in extra+def test_preflight_and_build_request_issue_206_post_tool_text(nim_provider):+ """Regression: assistant message with tool_use then text plus tool results (GitHub #206)."""+ tool_id = "toolu_issue_206"+ req = MockRequest(+ messages=[+ MockMessage("user", "Use echo once."),+ MockMessage(+ "assistant",+ [+ MockBlock(+ type="tool_use",+ id=tool_id,+ name="echo_smoke",+ input={"value": "FCC_206"},+ ),+ MockBlock(+ type="text",+ text="Commentary after the tool row.",+ ),+ ],+ ),+ MockMessage(+ "user",+ [+ MockBlock(+ type="tool_result", tool_use_id=tool_id, content="FCC_206"+ ),+ MockBlock(type="text", text="What was echoed?"),+ ],+ ),+ ],+ )+ nim_provider.preflight_stream(req, thinking_enabled=False)+ body = nim_provider._build_request_body(req, thinking_enabled=False)+ assert "messages" in body+ assert any(m.get("role") == "tool" for m in body["messages"])++@pytest.mark.asyncioasync def test_stream_response_text(nim_provider):"""Test streaming text response."""diff --git a/tests/providers/test_streaming_errors.py b/tests/providers/test_streaming_errors.pyindex 085212bb..d272c8ed 100644--- a/tests/providers/test_streaming_errors.py+++ b/tests/providers/test_streaming_errors.py@@ -81,6 +81,18 @@ def _make_chunk(return chunk+def _make_tool_calls_chunk(*, name: str, arguments: str, tool_id: str, index: int = 0):+ """Single OpenAI-style tool_calls delta (starts a native streamed tool block)."""+ tc = MagicMock()+ tc.index = index+ tc.id = tool_id+ fn = MagicMock()+ fn.name = name+ fn.arguments = arguments+ tc.function = fn+ return _make_chunk(tool_calls=[tc])++async def _collect_stream(provider, request):"""Collect all SSE events from a stream."""return [e async for e in provider.stream_response(request)]@@ -109,6 +121,23 @@ def _assert_no_content_deltas_after_error_text()+def _assert_error_not_in_text_deltas_after_tool(+ events: list[str], error_substr: str+) -> None:+ """Transport errors after a native tool call must not use assistant text_delta (issue #206)."""+ blob = "".join(events)+ for ev in parse_sse_text(blob):+ if ev.event != "content_block_delta":+ continue+ delta = ev.data.get("delta", {})+ if delta.get("type") == "text_delta" and error_substr in str(+ delta.get("text", "")+ ):+ raise AssertionError(+ f"error leaked as text_delta after tool_use: {ev.data!r} full={blob!r}"+ )++class TestStreamingExceptionHandling:"""Tests for error paths during stream_response."""@@ -209,6 +238,41 @@ class TestStreamingExceptionHandling:assert "message_stop" in event_text_assert_no_content_deltas_after_error_text(events, "Connection lost")+ @pytest.mark.asyncio+ async def test_error_after_native_tool_call_uses_top_level_error_event(self):+ """After a streamed tool_call, do not append error text as a new assistant text block."""+ provider = _make_provider()+ request = _make_request()+ tool_chunk = _make_tool_calls_chunk(+ name="echo_smoke", arguments="{}", tool_id="call_206", index=0+ )+ stream_mock = AsyncStreamMock(+ [tool_chunk], error=RuntimeError("Connection lost after tool")+ )+ with (+ patch.object(+ provider._client.chat.completions,+ "create",+ new_callable=AsyncMock,+ return_value=stream_mock,+ ),+ patch.object(+ provider._global_rate_limiter,+ "wait_if_blocked",+ new_callable=AsyncMock,+ return_value=False,+ ),+ ):+ events = await _collect_stream(provider, request)+ event_text = "".join(events)+ assert "tool_use" in event_text+ assert "Connection lost after tool" in event_text+ assert "event: error\n" in event_text+ assert "message_stop" in event_text+ _assert_error_not_in_text_deltas_after_tool(+ events, "Connection lost after tool"+ )+@pytest.mark.asyncioasync def test_empty_response_gets_space(self):"""Empty response with no text/tools gets a single space text block."""