From 46c25e7f1749d1bb97c3296270c37bf805ec0640 Mon Sep 17 00:00:00 2001 From: top9th <150784646+top9th@users.noreply.github.com> Date: Tue, 16 Sep 2025 13:12:50 +0800 Subject: [PATCH] Fix Google Gemini OpenAI compatibility issues (#268) Gemini is ready to use with this configuration. ``` OPENAI_API_KEY="gemini_key" OPENAI_BASE_URL="gemini_openai_compatible_url" CAI_MODEL=openai/gemini-2.5-pro ``` --- src/cai/sdk/agents/models/openai_chatcompletions.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cai/sdk/agents/models/openai_chatcompletions.py b/src/cai/sdk/agents/models/openai_chatcompletions.py index 2e28e8e8..f09ea929 100644 --- a/src/cai/sdk/agents/models/openai_chatcompletions.py +++ b/src/cai/sdk/agents/models/openai_chatcompletions.py @@ -12,6 +12,7 @@ from collections.abc import AsyncIterator, Iterable from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any, Literal, cast, overload +import uuid import litellm import tiktoken from openai import NOT_GIVEN, AsyncOpenAI, AsyncStream, NotGiven @@ -969,6 +970,15 @@ class OpenAIChatCompletionsModel(Model): if not hasattr(self, "_pending_tool_calls"): self._pending_tool_calls = {} + + # Fix Google Gemini OpenAI compatibility issues. + # When using the OpenAI-compatible API to call tools with Google Gemini + # tool_call.id is returned as an empty string. + if "openai/gemini" in os.getenv("CAI_MODEL"): + for tool_call in assistant_msg.tool_calls: + if tool_call.id is None or tool_call.id == "": + tool_call.id = uuid.uuid4().hex[:16] + for tool_call in assistant_msg.tool_calls: # Handle empty arguments before storing tool_args = tool_call.function.arguments