openai-python icon indicating copy to clipboard operation
openai-python copied to clipboard

strict tool schema enforcement

Open richardhundt opened this issue 1 year ago • 6 comments

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • [x] This is an issue with the Python library

Describe the bug

The OpenAI API allows non-strict tool schemas, but in validate_input_tools you just raise an exception if strict is not true. I've ended up writing my own client using httpx but I'd prefer to not have to maintain it, given that this just seems like an oversight. You're adding constraints which aren't present in the API.

To Reproduce

Just pass any non-strict tool schema.

Code snippets


OS

macOS

Python version

Python 3.10

Library version

all versions

richardhundt avatar Sep 14 '25 16:09 richardhundt

We only validate tools are strict in the .parse() method, as that's the entire point of the method, if you want to use non strict tools you should use .create()

RobertCraigie avatar Sep 14 '25 16:09 RobertCraigie

No you don't. You validate them somewhere down the stream call stack. I'm not calling parse.

EDIT: yep, it's right at the top of stream. But okay, I'll use create. Thanks for the tip.

richardhundt avatar Sep 14 '25 18:09 richardhundt

https://github.com/openai/openai-python/blob/4756247cee3d9548397b26a29109e76cc9522379/src/openai/resources/chat/completions/completions.py#L2879

richardhundt avatar Sep 14 '25 18:09 richardhundt

Another thing you might want to look at is the documentation for .parse()

        This method will also automatically parse `function` tool calls if:
        - You use the `openai.pydantic_function_tool()` helper method
        - You mark your tool schema with `"strict": True`

That reads as if you can opt out by not marking the tool schema with "strict": True

richardhundt avatar Sep 14 '25 19:09 richardhundt

and .parse() would probably also break under these conditions:

>>> class Foo(BaseModel):
...   model_config=ConfigDict(extra="allow")
...   x: float
... 
>>> foo = Foo(x=4.2, y=6.9)
>>> foo.x
4.2
>>> foo.y
6.9
>>> Foo.model_json_schema()
{'additionalProperties': True, 'properties': {'x': {'title': 'X', 'type': 'number'}}, 'required': ['x'], 'title': 'Foo', 'type': 'object'}

richardhundt avatar Sep 14 '25 19:09 richardhundt

The .parse() method used with Pydantic models can break if the model allows extra fields (extra="allow").

Example:

class Foo(BaseModel): model_config = ConfigDict(extra="allow") x: float

foo = Foo(x=4.2, y=6.9) # 'y' is an extra field

In this case, .parse() does not handle extra fields properly and may cause an error.


Quick fix

  1. If extra fields are not needed, disable extra="allow":

class Foo(BaseModel): model_config = ConfigDict(extra="forbid") # or default x: float

  1. If extra fields are needed, use the .model_validate() method instead of .parse():

foo = Foo.model_validate({'x': 4.2, 'y': 6.9}) print(foo.x) # 4.2 print(foo.y) # 6.9

This way, the model won’t break, and the extra fields are preserved.

nomiveritas avatar Sep 16 '25 14:09 nomiveritas