You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I ran into an issue when taking the JSON Schema that's generated from a msgspec.Struct and passing it to OpenAI's function_call APIs, which takes a json schema as an input to define how to produce the output. It doesn't like how msgspec produces the schema because there isn't a type field at the root level.
Below are two versions of JSON schemas generated from the same model (i.e., same fields). The first one is from msgspec, while the second one is from pydantic v2, which works fine with the openai API. I'm not sure which is more correct, but wanted to raise the issue in case it is something that the author can/wants to address.
BadRequestError: Error code: 400 - {'error': {'message': 'Invalid schema for function \'menu_items\': schema must be a JSON Schema of \'type: "object"\', got \'type: "None"\'.', 'type': 'invalid_request_error', 'param': None, 'code': None}}
The text was updated successfully, but these errors were encountered:
I don't think the type is required in the root schema, it's definitely not required anywhere else.
Sure, using $ref for root element seems like an unnecessary indirection, but as long it makes the code simpler (and Jim happier), I think it's fine.
I'd suggest reporting the problem with your service provider. You can also update the schema in your code, after all it's a representation of a dictionary.
Thanks for opening this @yqiang. Since object types are potentially cyclic, we always use "$ref" to refer to them by reference. We could make a change to avoid doing this for acyclic object types (the common case), but since the JSON schema spec allows it I'd rather not.
I suspect that openai's code here has a naive check for an object type - what happens if you add a type key in the top-level and do nothing else?
Question
I ran into an issue when taking the JSON Schema that's generated from a
msgspec.Struct
and passing it to OpenAI's function_call APIs, which takes a json schema as an input to define how to produce the output. It doesn't like how msgspec produces the schema because there isn't atype
field at the root level.Below are two versions of JSON schemas generated from the same model (i.e., same fields). The first one is from msgspec, while the second one is from pydantic v2, which works fine with the openai API. I'm not sure which is more correct, but wanted to raise the issue in case it is something that the author can/wants to address.
Thanks again for such a great library!
msgspec version 0.18.6, pydantic version 2.6.0.
msgspec generated json schema
pydantic generated schema
Relevant exception from the openai python SDK:
The text was updated successfully, but these errors were encountered: