mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-11 00:58:30 +00:00
Fix Gemini API additionalProperties error (issue #2457)
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -316,7 +316,19 @@ class LLM:
|
|||||||
messages = [{"role": "user", "content": messages}]
|
messages = [{"role": "user", "content": messages}]
|
||||||
formatted_messages = self._format_messages_for_provider(messages)
|
formatted_messages = self._format_messages_for_provider(messages)
|
||||||
|
|
||||||
# --- 2) Prepare the parameters for the completion call
|
# --- 2) If using Gemini, ensure additionalProperties is not in tool schemas
|
||||||
|
if tools and "gemini" in self.model.lower():
|
||||||
|
for i, tool in enumerate(tools):
|
||||||
|
if (
|
||||||
|
isinstance(tool, dict)
|
||||||
|
and "function" in tool
|
||||||
|
and "parameters" in tool["function"]
|
||||||
|
):
|
||||||
|
params = tool["function"]["parameters"]
|
||||||
|
if "additionalProperties" in params:
|
||||||
|
del params["additionalProperties"]
|
||||||
|
|
||||||
|
# --- 3) Prepare the parameters for the completion call
|
||||||
params = {
|
params = {
|
||||||
"model": self.model,
|
"model": self.model,
|
||||||
"messages": formatted_messages,
|
"messages": formatted_messages,
|
||||||
|
|||||||
@@ -240,6 +240,26 @@ class CrewStructuredTool:
|
|||||||
"""Get the tool's input arguments schema."""
|
"""Get the tool's input arguments schema."""
|
||||||
return self.args_schema.model_json_schema()["properties"]
|
return self.args_schema.model_json_schema()["properties"]
|
||||||
|
|
||||||
|
def to_openai_function(self) -> dict:
|
||||||
|
"""Convert the tool to an OpenAI function format.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
dict: A dictionary in the OpenAI function format.
|
||||||
|
"""
|
||||||
|
schema = self.args_schema.model_json_schema()
|
||||||
|
# Remove additionalProperties field to prevent Gemini API errors
|
||||||
|
if "additionalProperties" in schema:
|
||||||
|
del schema["additionalProperties"]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": self.name,
|
||||||
|
"description": self.description,
|
||||||
|
"parameters": schema
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return (
|
return (
|
||||||
f"CrewStructuredTool(name='{self.name}', description='{self.description}')"
|
f"CrewStructuredTool(name='{self.name}', description='{self.description}')"
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def schema_class():
|
|||||||
return TestSchema
|
return TestSchema
|
||||||
|
|
||||||
|
|
||||||
class InternalCrewStructuredTool:
|
class TestInternalCrewStructuredTool:
|
||||||
def test_initialization(self, basic_function, schema_class):
|
def test_initialization(self, basic_function, schema_class):
|
||||||
"""Test basic initialization of CrewStructuredTool"""
|
"""Test basic initialization of CrewStructuredTool"""
|
||||||
tool = CrewStructuredTool(
|
tool = CrewStructuredTool(
|
||||||
@@ -144,3 +144,30 @@ class InternalCrewStructuredTool:
|
|||||||
{"required_param": "test", "optional_param": "custom", "nullable_param": 42}
|
{"required_param": "test", "optional_param": "custom", "nullable_param": 42}
|
||||||
)
|
)
|
||||||
assert result == "test custom 42"
|
assert result == "test custom 42"
|
||||||
|
|
||||||
|
def test_to_openai_function_no_additional_properties(self):
|
||||||
|
"""Test that the to_openai_function method doesn't include additionalProperties."""
|
||||||
|
|
||||||
|
class TestSchema(BaseModel):
|
||||||
|
test_field: str = Field(..., description="A test field")
|
||||||
|
|
||||||
|
def test_func(test_field: str) -> str:
|
||||||
|
"""Test function that returns the input."""
|
||||||
|
return f"Test function received: {test_field}"
|
||||||
|
|
||||||
|
tool = CrewStructuredTool(
|
||||||
|
name="test_tool",
|
||||||
|
description="A test tool",
|
||||||
|
args_schema=TestSchema,
|
||||||
|
func=test_func
|
||||||
|
)
|
||||||
|
|
||||||
|
function_dict = tool.to_openai_function()
|
||||||
|
assert "additionalProperties" not in function_dict["function"]["parameters"]
|
||||||
|
|
||||||
|
# Verify other properties are correct
|
||||||
|
assert function_dict["type"] == "function"
|
||||||
|
assert function_dict["function"]["name"] == "test_tool"
|
||||||
|
assert function_dict["function"]["description"] == "A test tool"
|
||||||
|
assert "properties" in function_dict["function"]["parameters"]
|
||||||
|
assert "test_field" in function_dict["function"]["parameters"]["properties"]
|
||||||
|
|||||||
Reference in New Issue
Block a user