fix: preserve Gemini thought_signature in tool calls and add ddgs dependency #26
fix: preserve Gemini thought_signature in tool calls and add ddgs dependency #26SoufianoDev wants to merge 1 commit into
Conversation
|
Hi @SoufianoDev, thanks for the thorough PR and the detailed tests! Unfortunately, this approach won't work due to a limitation in When the formatted assistant message (with # langchain-openai/chat_models/base.py
tool_call_supported_props = {"id", "type", "function"}
message_dict["tool_calls"] = [
{k: v for k, v in tool_call.items() if k in tool_call_supported_props}
for tool_call in message_dict["tool_calls"]
]So even though the PR correctly captures and re-attaches We've documented this on the issue (#25) — this is an upstream LangChain limitation that can't be worked around from the application side. The current recommendation for Gemini users is to switch to a provider that doesn't require The Appreciate the effort — happy to revisit if LangChain adds support for passthrough fields! |
Summary
Fixes #25 — two bugs that cause Gemini provider to fail when using tool calls.
Bug 1: Missing
ddgsdependencyWebSearchToolimportsddgs/duckduckgo_searchat runtime, but the package was not listed inpyproject.tomloragent/requirements.txt. This causedweb_searchto always return a JSON error, which then triggered Bug 2.Bug 2: Gemini
thought_signaturenot preserved across turnsGemini's API returns
extra_content.google.thought_signatureon tool call responses. This signature must be echoed back in subsequent conversation turns. However,ChatOpenAIfromlangchain-openaistripsextra_contentduring parsing. When the conversation history was sent back to Gemini without the signature, it returned a400 INVALID_ARGUMENTerror:Changes
pyproject.toml/agent/requirements.txt— Addedddgs>=6.0.0dependencyagent/src/providers/llm.py— AddedGeminiChatOpenAIsubclass ofChatOpenAIthat overrides_create_chat_result()to captureextra_contentfrom raw API responses and store it inAIMessage.additional_kwargs["tool_call_extras"]. Updatedbuild_llm()to use this class whenLANGCHAIN_PROVIDER=gemini.agent/src/providers/chat.py— Addedthought_signaturefield toToolCallRequest. Updated_parse_response()to extract signatures fromtool_call_extras. Updatedstream_chat()to bypass streaming for Gemini (since stream chunks don't preserveextra_content) and usechat()directly.agent/src/agent/context.py— Updatedformat_assistant_tool_calls()to includeextra_content.google.thought_signaturein formatted assistant messages when present.agent/tests/test_gemini_thought_signature.py— Added 23 tests covering the full thought_signature preservation flow, including end-to-end tests with real OpenAI SDK Pydantic models.Testing
WebSearchToolwithddgs