Skip to content

Commit 543d192

Browse files
committed
refactor: more specific code path for thinking block insertion
Move the location of thinking block insertion from `ensure_assistant_message()` to `elif func_call := cls.maybe_function_tool_call(item):` to be more specific and prevent unintended side-effects.
1 parent 54a9f7a commit 543d192

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/agents/models/chatcmpl_converter.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,6 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
356356
current_assistant_msg = ChatCompletionAssistantMessageParam(role="assistant")
357357
current_assistant_msg["tool_calls"] = []
358358

359-
# If we have pending thinking blocks, use them as the content
360-
# This is required for Anthropic API tool calls with interleaved thinking
361-
if pending_thinking_blocks:
362-
current_assistant_msg["content"] = pending_thinking_blocks # type: ignore
363-
pending_thinking_blocks = None # Clear after using
364-
365359
return current_assistant_msg
366360

367361
for item in items:
@@ -477,6 +471,13 @@ def ensure_assistant_message() -> ChatCompletionAssistantMessageParam:
477471

478472
elif func_call := cls.maybe_function_tool_call(item):
479473
asst = ensure_assistant_message()
474+
475+
# If we have pending thinking blocks, use them as the content
476+
# This is required for Anthropic API tool calls with interleaved thinking
477+
if pending_thinking_blocks:
478+
asst["content"] = pending_thinking_blocks # type: ignore
479+
pending_thinking_blocks = None # Clear after using
480+
480481
tool_calls = list(asst.get("tool_calls", []))
481482
arguments = func_call["arguments"] if func_call["arguments"] else "{}"
482483
new_tool_call = ChatCompletionMessageFunctionToolCallParam(

0 commit comments

Comments
 (0)