googleapis / googleapis/python-genai

`const` keyword is not supported in function definitions

Aperta
#1,093 8 commenti 0 reazioni 1 assegnatario Rivendicata da @Venkaiahbabuneelam Vedi su GitHub
priority: p2 type: feature request
Lingua principale
Python
Stelle
4k
Fork
1k
Merge medio
2g 12h
PR unite (30g)
41

Descrizione

The `const` JSONSchema keyword is not supported in function definitions. The `const` keyword is very useful for type discriminations. To repro the issue, do the following
```
import google.genai as genai

tool_definition = {
"name": "get_weather",
"description": "Get the weather",
"parameters": {
"properties": {
"type": {"description": "the type", "type": "string", "const": "city"},
"value": {
"description": "The value to get the weather for",
"type": "string",
},
},
"required": ["type", "value"],
"type": "object",
},
}

client = genai.Client()
response = client.models.generate_content(
model="gemini-2.5-flash",
contents=[
{
"role": "user",
"parts": [{"text": "whats the weather in the city of Tokyo?"}],
}
],
config={"tools": [{"function_declarations": [tool_definition]}]},
)
```
this will produce this error
```
ValidationError: 5 validation errors for GenerateContentConfig
tools.0.Tool.function_declarations.0.parameters.properties.type.const
Extra inputs are not permitted [type=extra_forbidden, input_value='city', input_type=str]
For further information visit https://errors.pydantic.dev/2.11/v/extra_forbidden
tools.0.callable
Input should be callable [type=callable_type, input_value={'function_declarations':...'], 'type': 'object'}}]}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/callable_type
tools.0.Tool.name
Field required [type=missing, input_value={'function_declarations':...'], 'type': 'object'}}]}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing
tools.0.Tool.inputSchema
Field required [type=missing, input_value={'function_declarations':...'], 'type': 'object'}}]}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/missing
tools.0.is-instance[ClientSession]
Input should be an instance of ClientSession [type=is_instance_of, input_value={'function_declarations':...'], 'type': 'object'}}]}, input_type=dict]
For further information visit https://errors.pydantic.dev/2.11/v/is_instance_of
```

I know that this can addressed with an enum workaround
```
{"description": "the type", "type": "string", "enum": ["city"]}
```

but I prefer not because it increases token count. Plus, all other major AI providers (openai, anthropic, etc.) support `const` in function definitions

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.