diff --git a/pydantic_ai_slim/pydantic_ai/models/openai.py b/pydantic_ai_slim/pydantic_ai/models/openai.py index dc1026f9f..d6a76de80 100644 --- a/pydantic_ai_slim/pydantic_ai/models/openai.py +++ b/pydantic_ai_slim/pydantic_ai/models/openai.py @@ -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'] = '#' diff --git a/tests/models/test_openai.py b/tests/models/test_openai.py index b19c03b6c..0e3f6fe99 100644 --- a/tests/models/test_openai.py +++ b/tests/models/test_openai.py @@ -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', }, @@ -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', } }, @@ -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', } }, @@ -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', } },