-
Notifications
You must be signed in to change notification settings - Fork 186
fix(anthropic,gemini): preserve provider-specific reasoning signatures across turns #1153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,18 @@ def _extract_usage_dict(response: types.GenerateContentResponse) -> dict[str, An | |
| return usage | ||
|
|
||
|
|
||
| def _thought_signature_extra_content(part: Any) -> dict[str, Any] | None: | ||
| """Build the OpenAI-compatible extra_content payload for a Gemini thought_signature, if present. | ||
|
|
||
| Shared by both the non-streaming (_convert_response_to_response_dict) and streaming | ||
| (_create_openai_chunk_from_google_chunk) conversion paths so they stay in sync. | ||
| """ | ||
| thought_signature = getattr(part, "thought_signature", None) | ||
| if thought_signature is not None and isinstance(thought_signature, bytes): | ||
| return {"google": {"thought_signature": base64.b64encode(thought_signature).decode("utf-8")}} | ||
| return None | ||
|
Comment on lines
+279
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# Inspect the target file around the reported lines.
sed -n '240,320p' src/any_llm/providers/gemini/utils.py
# Find where types.Part is imported and used in this file.
rg -n "types\.Part|from .* import .*types|import .*types" src/any_llm/providers/gemini/utils.py
# Locate the definition of Part in the repository, if present.
fd -a "types.py" src any_llm . | sed -n '1,40p'
rg -n "class Part|def .*thought_signature|thought_signature" src any_llm . -g '!**/.git/**'Repository: mozilla-ai/any-llm Length of output: 11963 🌐 Web query:
💡 Result: In the Google Gen AI ecosystem, Citations:
🏁 Script executed: #!/bin/bash
set -euo pipefail
sed -n '80,150p' src/any_llm/types/completion.py
sed -n '860,930p' tests/unit/providers/test_gemini_provider.py
sed -n '980,1065p' tests/unit/providers/test_gemini_provider.pyRepository: mozilla-ai/any-llm Length of output: 8962 🌐 Web query:
💡 Result: In the Google Gen AI Python SDK ( Citations:
Use The helper only receives 🧰 Tools🪛 Ruff (0.15.20)[warning] 279-279: Dynamically typed expressions (typing.Any) are disallowed in (ANN401) 🤖 Prompt for AI AgentsSources: Coding guidelines, Linters/SAST tools |
||
|
|
||
|
|
||
| def _convert_response_to_response_dict(response: types.GenerateContentResponse) -> dict[str, Any]: | ||
| response_dict = { | ||
| "id": "google_genai_response", | ||
|
|
@@ -315,11 +327,8 @@ def _convert_response_to_response_dict(response: types.GenerateContentResponse) | |
| } | ||
|
|
||
| # Include thought_signature if present (OpenAI compatibility format) | ||
| thought_signature = getattr(part, "thought_signature", None) | ||
| if thought_signature is not None and isinstance(thought_signature, bytes): | ||
| tool_call_dict["extra_content"] = { | ||
| "google": {"thought_signature": base64.b64encode(thought_signature).decode("utf-8")} | ||
| } | ||
| if extra_content := _thought_signature_extra_content(part): | ||
| tool_call_dict["extra_content"] = extra_content | ||
|
|
||
| tool_calls_list.append(tool_call_dict) | ||
| elif getattr(part, "text", None): | ||
|
|
@@ -405,6 +414,10 @@ def _create_openai_chunk_from_google_chunk( | |
| for key, value in args.items(): | ||
| args_dict[key] = value | ||
|
|
||
| # Include thought_signature if present (OpenAI compatibility format), mirroring | ||
| # the non-streaming conversion in _convert_response_to_response_dict. | ||
| extra_content = _thought_signature_extra_content(part) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| tool_calls_list.append( | ||
| ChoiceDeltaToolCall( | ||
| index=len(tool_calls_list), | ||
|
|
@@ -414,6 +427,7 @@ def _create_openai_chunk_from_google_chunk( | |
| name=function_call.name, | ||
| arguments=json.dumps(args_dict), | ||
| ), | ||
| extra_content=extra_content, | ||
| ) | ||
| ) | ||
| elif part.text: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.