Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ Newest first. `Unreleased` is what is on `main` and not yet tagged.

## Unreleased

- **A Bot on Gemini 3 or later finishes what it started with a tool, on every engine.** 1.2.2 fixed
this on the LangGraph engines. On Google ADK, AG2, CrewAI, Langroid, Mastra and Strands, a Bot on a
Gemini key saved in Settings or OpenBot Desktop, or on OpenBot Cloud's AI connection, still stopped
once a tool's result came back, with Google's "Function call is missing a thought_signature". CrewAI
now sends each call back with the signature Gemini gave it, as LangGraph does, and so does ADK when
the same process answers the result. Where an engine cannot keep it, the call goes with Google's
documented placeholder for a call whose signature is not available. Requests to every other
provider and model are sent as before.

## 1.2.3

**Before upgrading.**
Expand Down
2 changes: 1 addition & 1 deletion agent-adk/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-adk/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-adk/src ./src
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/progress_reminder.py shared/empty_turns.py /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/gemini_signatures.py shared/progress_reminder.py shared/empty_turns.py /shared/

ENV PORT=4208
EXPOSE 4208
Expand Down
7 changes: 7 additions & 0 deletions agent-adk/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
from progress_reminder import with_litellm_reminders
from model_providers import bot_settings, litellm_model_id
from run_model_key import RunModelKey
import gemini_signatures

# A tool call handed back to Gemini 3 by a process that did not make it goes with Google's
# placeholder for its thought signature: `ag_ui_adk` sends no signature out with a client tool's
# call, so the thread has none to bring back. The process that made the call keeps the real one
# in its session. See gemini_signatures.py.
gemini_signatures.install()

from ag_ui.core import (
AssistantMessage,
Expand Down
92 changes: 92 additions & 0 deletions agent-adk/tests/test_gemini_signatures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""Which model requests `shared/gemini_signatures.py` changes, and which it sends as they were.

The module is shared by the Python engines; its rule is checked once, here, on the requests the
engines send. Each engine's `test_gemini_thought_signature.py` checks a run through it.
"""

import json
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from gemini_signatures import GEMINI_SKIP_SIGNATURE, checks_signatures, signed_body

GOOGLE = "https://generativelanguage.googleapis.com/v1beta/openai/chat/completions"
GATEWAY = "http://openbot:3010/api/model-provider/v1/chat/completions"


def chat(model="gemini-3.8-flash", calls=1, signature=None):
tool_calls = [
{"id": f"c{n}", "type": "function", "function": {"name": "f", "arguments": "{}"}}
for n in range(calls)
]
if signature:
tool_calls[0]["extra_content"] = {"google": {"thought_signature": signature}}
return json.dumps(
{
"model": model,
"messages": [
{"role": "user", "content": "hi"},
{"role": "assistant", "content": None, "tool_calls": tool_calls},
*({"role": "tool", "tool_call_id": f"c{n}", "content": "{}"} for n in range(calls)),
],
}
).encode()


def signatures(body):
return [
((call.get("extra_content") or {}).get("google") or {}).get("thought_signature")
for message in json.loads(body)["messages"]
for call in message.get("tool_calls") or []
]


@pytest.mark.parametrize(
"model", ["gemini-3.8-flash", "gemini-4-hydrogen", "models/gemini-3.6-flash", "google/gemini-4-neon"]
)
def test_gemini_3_on_checks_signatures(model):
assert checks_signatures(model)


@pytest.mark.parametrize(
"model", ["gemini-2.5-flash", "gemini-1.5-pro", "gemma-4-31b-it", "gpt-5.5", "claude-sonnet-5-5", "", None]
)
def test_other_models_do_not(model):
assert not checks_signatures(model)


@pytest.mark.parametrize("url", [GOOGLE, GATEWAY])
def test_the_first_call_of_an_unsigned_step_gets_the_placeholder(url):
assert signatures(signed_body("POST", url, chat(calls=2))) == [GEMINI_SKIP_SIGNATURE, None]


def test_a_real_signature_is_sent_as_it_was():
assert signed_body("POST", GOOGLE, chat(signature="real")) is None


@pytest.mark.parametrize(
"url",
[
"https://api.openai.com/v1/chat/completions",
"https://openrouter.ai/api/v1/chat/completions",
"http://127.0.0.1:11434/v1/chat/completions",
],
)
def test_another_provider_is_sent_its_request_as_it_was(url):
assert signed_body("POST", url, chat()) is None


def test_a_model_that_does_not_check_is_sent_its_request_as_it_was():
assert signed_body("POST", GOOGLE, chat(model="gemini-2.5-flash")) is None
assert signed_body("POST", GATEWAY, chat(model="gpt-5.5")) is None


def test_anything_but_a_chat_with_calls_is_sent_as_it_was():
assert signed_body("GET", GOOGLE, chat()) is None
assert signed_body("POST", GOOGLE.replace("chat/completions", "embeddings"), chat()) is None
assert signed_body("POST", GOOGLE, b"not json") is None
plain = json.dumps({"model": "gemini-3.8-flash", "messages": [{"role": "user", "content": "hi"}]})
assert signed_body("POST", GOOGLE, plain.encode()) is None
45 changes: 45 additions & 0 deletions agent-adk/tests/test_gemini_thought_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"""A Bot on Gemini 3 finishes a tool call the server ran between two runs.

Gemini refuses the run after a tool's result unless the call comes back with the thought signature
it gave it ("Function call is missing a thought_signature"). See `check_gemini_handback`.
"""

import importlib
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from harness_contract import check_gemini_handback
from gemini_signatures import GEMINI_SKIP_SIGNATURE

TOKEN = "contract-token"


@pytest.fixture
def app_for(monkeypatch):
def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
monkeypatch.setenv("OTEL_SDK_DISABLED", "true")
for key, value in environment.items():
monkeypatch.setenv(key, value)
from src import main

return importlib.reload(main).app

return app_for


def test_the_process_that_made_the_call_sends_back_its_signature(app_for):
state = check_gemini_handback(app_for, token=TOKEN, warm=True)
# ADK's session keeps the signature Gemini gave the call.
assert state["returned"] == list(state["issued"].values()), state


def test_a_fresh_process_sends_the_call_back_with_googles_placeholder(app_for):
state = check_gemini_handback(app_for, token=TOKEN, warm=False)
# `ag_ui_adk` sends no signature out with a client tool's call, so the thread brings none back.
assert state["returned"] == [GEMINI_SKIP_SIGNATURE], state
2 changes: 1 addition & 1 deletion agent-ag2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-ag2/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-ag2/src ./src
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/progress_reminder.py /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/gemini_signatures.py shared/progress_reminder.py /shared/

ENV PORT=4210
EXPOSE 4210
Expand Down
6 changes: 6 additions & 0 deletions agent-ag2/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import bot_settings, openai_api
from run_model_key import RunModelKey
import gemini_signatures

# A tool call handed back to Gemini 3 goes with Google's placeholder for its thought signature,
# which AG2 keeps between runs only on its own Gemini config, not on an OpenAI-compatible
# endpoint. See gemini_signatures.py.
gemini_signatures.install()

from ag2 import Agent
from ag2.ag_ui import AGUIStream
Expand Down
41 changes: 41 additions & 0 deletions agent-ag2/tests/test_gemini_thought_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""A Bot on Gemini 3 finishes a tool call the server ran between two runs.

Gemini refuses the run after a tool's result unless the call comes back with the thought signature
it gave it ("Function call is missing a thought_signature"). See `check_gemini_handback`.
"""

import importlib
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from harness_contract import check_gemini_handback
from gemini_signatures import GEMINI_SKIP_SIGNATURE

TOKEN = "contract-token"


@pytest.fixture
def app_for(monkeypatch):
def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
for key, value in environment.items():
monkeypatch.setenv(key, value)
from src import main

return importlib.reload(main).app

return app_for


@pytest.mark.parametrize("warm", [True, False], ids=["same-process", "fresh-process"])
def test_a_tool_call_on_gemini_goes_back_signed(app_for, warm):
state = check_gemini_handback(app_for, token=TOKEN, warm=warm)
# The integration does not carry the signature between runs: Google's placeholder goes in
# its place.
assert state["returned"] == [GEMINI_SKIP_SIGNATURE], state
2 changes: 1 addition & 1 deletion agent-crewai/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-crewai/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-crewai/src ./src
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/progress_reminder.py shared/empty_turns.py /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/gemini_signatures.py shared/progress_reminder.py shared/empty_turns.py /shared/

ENV PORT=4202
EXPOSE 4202
Expand Down
48 changes: 39 additions & 9 deletions agent-crewai/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
from progress_reminder import with_litellm_reminders
from model_providers import bot_settings, litellm_model_id
from run_model_key import RunModelKey
import gemini_signatures

# A call the thread brings back with no thought signature goes to Gemini 3 with Google's
# placeholder. One it brings back with its signature goes with that (`_sealed`).
gemini_signatures.install()

import ag_ui_crewai.endpoint as crewai_endpoint
from ag_ui.core import Message, Tool
Expand Down Expand Up @@ -89,22 +94,47 @@ def _strip_none(message: dict[str, Any]) -> dict[str, Any]:
return {key: value for key, value in message.items() if value is not None}


def _google_signature(tool_call: Mapping) -> str | None:
"""Gemini's thought signature on a call from its OpenAI-compatible endpoint, if it gave one."""
extra = tool_call.get("extra_content")
google = extra.get("google") if isinstance(extra, Mapping) else None
signature = google.get("thought_signature") if isinstance(google, Mapping) else None
return signature if isinstance(signature, str) and signature else None


def _sealed(message: dict[str, Any]) -> dict[str, Any]:
"""The model's message, each call Gemini signed carrying its signature as its `encryptedValue`.

Gemini 3 refuses the run that brings a tool's result unless the call comes back with the
thought signature it gave it. The call leaves in the run's snapshot and comes back in the
thread, which keeps a call's `encryptedValue` and nothing else a provider put on it.
"""
for tool_call in message.get("tool_calls") or []:
if isinstance(tool_call, dict) and _google_signature(tool_call):
tool_call["encrypted_value"] = _google_signature(tool_call)
return message


def _provider_tool_calls(tool_calls: Any) -> Any:
if not isinstance(tool_calls, list):
return deepcopy(tool_calls)
# A call's `encryptedValue` goes back only to Gemini, the one provider that can read it.
gemini = gemini_signatures.checks_signatures(_model())
projected = []
for tool_call in tool_calls:
if not isinstance(tool_call, Mapping):
tool_call = tool_call.model_dump()
projected.append(
_strip_none(
{
key: deepcopy(tool_call[key])
for key in ("id", "type", "function")
if key in tool_call
}
)
call = _strip_none(
{
key: deepcopy(tool_call[key])
for key in ("id", "type", "function")
if key in tool_call
}
)
sealed = tool_call.get("encrypted_value") or tool_call.get("encryptedValue")
if gemini and isinstance(sealed, str) and sealed:
call["extra_content"] = {"google": {"thought_signature": sealed}}
projected.append(call)
return projected


Expand Down Expand Up @@ -182,7 +212,7 @@ async def answer(self):
stream=False,
)
self.state.setdefault("messages", []).append(
response.choices[0].message.model_dump()
_sealed(response.choices[0].message.model_dump())
)


Expand Down
49 changes: 49 additions & 0 deletions agent-crewai/tests/test_gemini_thought_signature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""A Bot on Gemini 3 finishes a tool call the server ran between two runs.

Gemini refuses the run after a tool's result unless the call comes back with the thought signature
it gave it ("Function call is missing a thought_signature"). See `check_gemini_handback`.
"""

import importlib
import sys
from pathlib import Path

import pytest

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from harness_contract import check_gemini_handback
from gemini_signatures import GEMINI_SKIP_SIGNATURE

TOKEN = "contract-token"


@pytest.fixture
def app_for(monkeypatch):
def app_for(environment):
monkeypatch.delenv("OPENAI_API_BASE", raising=False)
monkeypatch.delenv("ANTHROPIC_AUTH_TOKEN", raising=False)
monkeypatch.setenv("MANAGED_AGENT_TOKEN", TOKEN)
for key, value in environment.items():
monkeypatch.setenv(key, value)
# `main` wraps `crewai_prepare_inputs` in the library's module when it is imported:
# restore the library first, then import `main` anew, as a fresh process does.
import ag_ui_crewai.endpoint

importlib.reload(ag_ui_crewai.endpoint)
sys.modules.pop("src.main", None)
return importlib.import_module("src.main").app

return app_for


@pytest.mark.parametrize("warm", [True, False], ids=["same-process", "fresh-process"])
def test_a_tool_call_on_gemini_goes_back_signed(app_for, warm):
state = check_gemini_handback(app_for, token=TOKEN, warm=warm)
# The signature Gemini gave: out on the call as its `encryptedValue`, back from the thread.
assert state["returned"] == list(state["issued"].values()), state


def test_a_call_the_thread_has_no_signature_for_goes_with_googles_placeholder(app_for):
state = check_gemini_handback(app_for, token=TOKEN, thread_keeps_signature=False)
assert state["returned"] == [GEMINI_SKIP_SIGNATURE], state
2 changes: 1 addition & 1 deletion agent-langroid/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ COPY agent-langroid/requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY agent-langroid/src ./src
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/progress_reminder.py shared/empty_turns.py /shared/
COPY shared/model_providers.py shared/model-providers.json shared/run_model_key.py shared/gemini_signatures.py shared/progress_reminder.py shared/empty_turns.py /shared/

ENV PORT=4209
EXPOSE 4209
Expand Down
5 changes: 5 additions & 0 deletions agent-langroid/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
sys.path.insert(0, str(Path(__file__).resolve().parents[2] / "shared"))
from model_providers import bot_settings, litellm_model_id, openai_api
from run_model_key import RunModelKey
import gemini_signatures

# A tool call handed back to Gemini 3 goes with Google's placeholder for its thought signature,
# which the integration does not carry between runs. See gemini_signatures.py.
gemini_signatures.install()

import litellm
from ag_ui_langroid import create_langroid_app
Expand Down
Loading
Loading