googleapis / googleapis/python-genai

Function calling incorrectly interprets title fields in nested JSON schemas as callable functions

Open
#1,732 6 comments 4 reactions 1 assignee Assigned to @kkorpal View on GitHub
priority: p2 type: bug
Dominant language
Python
Stars
4k
Forks
1k
Avg merge
2d 11h
Merged PRs (30d)
40

Description

When using the Google genai Python SDK for function calling with nested JSON schemas, Gemini incorrectly treats title fields in $defs definitions as callable Python functions, resulting in MALFORMED_FUNCTION_CALL errors.

#### Environment details

- Programming language: python
- OS: macos
- Language runtime version: 3.12
- Package version: 2.8.1

#### Steps to reproduce

```python
import asyncio
from google.genai import Client
from pydantic import BaseModel, Field
from pydantic.type_adapter import TypeAdapter

class NestedModel(BaseModel):
name: str = Field(description='Name of the item')
value: int = Field(description='Value of the item')

class MiddleModel(BaseModel):
title: str = Field(description='Title of the page')
items: list[NestedModel] = Field(description='List of nested items')

class TopModel(BaseModel):
name: str = Field(description='Name of the collection')
pages: list[MiddleModel] = Field(description='List of pages')

# Generate schema with $ref/$defs (standard Pydantic behavior)
schema = TypeAdapter(TopModel).json_schema()

config_dict = {
'tools': [
{
'function_declarations': [
{
'name': 'final_result',
'description': 'Return the final result',
'parameters_json_schema': schema,
}
]
}
],
'tool_config': {'function_calling_config': {'mode': 'ANY', 'allowed_function_names': ['final_result']}},
}

async def test():
client = Client(api_key='YOUR_API_KEY')

response = await client.aio.models.generate_content(
model='gemini-2.5-flash',
contents='Create a simple example with 2 pages',
config=config_dict,
)
print(response)

asyncio.run(test())

```

Expected output: Valid function call with JSON matching the schema
Actual output : MALFORMED_FUNCTION_CALL or attempts to call MiddleModel/NestedModel as functions
Making sure to follow these steps will guarantee the quickest resolution possible.

**Impact**

This prevents users from:
- Using standard Pydantic schema generation with nested models
- Leveraging $ref/$defs for schema reuse and organization
- Building complex, deeply nested structured output schemas

Users must either:
- Manually strip title fields from nested schemas (workaround)
- Use responseJsonSchema with native structured output instead (different API)
- Inline all nested schemas, losing the benefits of $ref/$defs`

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.