Skip to content

Retain defaults in non-strict openai schemas #1519

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion pydantic_ai_slim/pydantic_ai/models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,10 +992,18 @@ def walk(self) -> JsonSchema:
def transform(self, schema: JsonSchema) -> JsonSchema: # noqa C901
# Remove unnecessary keys
schema.pop('title', None)
schema.pop('default', None)
schema.pop('$schema', None)
schema.pop('discriminator', None)

default = schema.get('default', _sentinel)
if default is not _sentinel:
# the "default" keyword is not allowed in strict mode, but including it makes some Ollama models behave
# better, so we keep it around when not strict
if self.strict is True:
schema.pop('default', None)
elif self.strict is None:
self.is_strict_compatible = False

if schema_ref := schema.get('$ref'):
if schema_ref == self.root_ref:
schema['$ref'] = '#'
Expand Down
11 changes: 7 additions & 4 deletions tests/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,10 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
'$defs': {
'MyDefaultRecursiveDc': {
'properties': {
'field': {'anyOf': [{'$ref': '#/$defs/MyDefaultRecursiveDc'}, {'type': 'null'}]}
'field': {
'anyOf': [{'$ref': '#/$defs/MyDefaultRecursiveDc'}, {'type': 'null'}],
'default': None,
}
},
'type': 'object',
},
Expand Down Expand Up @@ -947,7 +950,7 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
{
'$defs': {
'MyDefaultDc': {
'properties': {'x': {'type': 'integer'}},
'properties': {'x': {'default': 1, 'type': 'integer'}},
'type': 'object',
}
},
Expand Down Expand Up @@ -987,7 +990,7 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
{
'$defs': {
'MyDefaultDc': {
'properties': {'x': {'type': 'integer'}},
'properties': {'x': {'default': 1, 'type': 'integer'}},
'type': 'object',
}
},
Expand Down Expand Up @@ -1027,7 +1030,7 @@ def tool_with_tuples(x: tuple[int], y: tuple[str] = ('abc',)) -> str:
{
'$defs': {
'MyDefaultDc': {
'properties': {'x': {'type': 'integer'}},
'properties': {'x': {'default': 1, 'type': 'integer'}},
'type': 'object',
}
},
Expand Down