Skip to content

Merge add_tool_call() tool calls into to_dict() output#1438

Open
CrypticCortex wants to merge 1 commit into
simonw:mainfrom
CrypticCortex:merge-tool-calls-into-to-dict
Open

Merge add_tool_call() tool calls into to_dict() output#1438
CrypticCortex wants to merge 1 commit into
simonw:mainfrom
CrypticCortex:merge-tool-calls-into-to-dict

Conversation

@CrypticCortex
Copy link
Copy Markdown

Summary

  • Read-time merge in _build_parts(): after Parts are assembled from stream events, append a ToolCallPart for each tool_call_id in self._tool_calls not already represented. None-id calls always append (Gemini omits the id on parallel calls).

    • Dedup-by-tool_call_id keeps the OpenAI plugin's "emit StreamEvents AND add_tool_call" pattern producing exactly one Part. test_text_then_tool_call_part_index_advances still passes.
    • Adds test_to_dict_includes_tool_calls_added_alongside_text (mirrors the llm-echo shape, fails on main) and test_to_dict_dedupes_tool_call_emitted_via_both_apis to TestResponseToDictFromDict.

    732 tests pass, ruff/black/mypy clean. Happy to switch to a DeprecationWarning from add_tool_call instead, if you'd rather nudge plugins t

When a plugin yielded any StreamEvent and also called
response.add_tool_call(), the tool call was silently dropped from
response.to_dict()["messages"] because Part assembly read only from
_stream_events. The _tool_calls fallback only fired when no events
existed at all (the rehydrate-from-SQLite path).

Fix: in _build_parts(), after assembling Parts from stream events,
walk self._tool_calls and append a ToolCallPart for any tool_call_id
not already represented. None-id calls always append, since some
providers (Gemini) omit the id on parallel tool calls.

Dedup-by-tool_call_id keeps the OpenAI plugin's "emit StreamEvents
AND add_tool_call" pattern working without producing duplicate Parts.

Refs simonw#1433
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant