I am trying to enforce a JSON schema as an output of an LLM. I found
this post
and have been trying to use my JSON schema. Is there no support to
oneOf
,
anyOf
, or any similar features of JSON schemas? Where can I find where all the documentation for this kind of expected formats is? I also tried using pydantic and BaseModel’s to replicate my JSON schema but ran into similar issues with more complex logic involving different combinations of requirements. I have basically been trying to use something like this:
my_schema = {
"type": "json_schema",
"json_schema": {
"name": "example_schema",
"strict": True,
"schema": {
"type": "object",
"properties": {
"A": {
"type": "string"
"B": {
"type": "number"
"C": {
"type": "array",
"items": {
"type": "string"
"oneOf": [
"required": ["A"]
"required": ["B"]
"required": ["C"]
"additionalProperties": False
completion = client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=messages, # this has a system prompt and user message
response_format=my_schema
Running this gives me the error:
BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'example': In context=(), 'oneOf' is not permitted.", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
Where can I find documentation for what formats are supported and what features are allowed? Also, is there a workaround for not being allowed to use oneOf and anyOf?
I can have an AI do a mind dump vs the documentation, and we can start to check off per data type keywords as yes or no – probably no if a more basic keyword specifically doesn’t work.
Current state of info below: just a few checked and noted.
(If supported) are speculative. I’ll update if you have feedback, but these kind of documentation threads quickly become bumped off and lost in the forum.
Based on the documentation and the list of unsupported keywords, here are the common and useful JSON Schema keywords that apply to each data type, which are not mentioned as unsupported and are commonly seen in JSON Schema:
anyOf is supported and an example of its use is available here.
oneOf is currently not supported! So you would need to use anyOf in its place.
One caveat with anyOf is that it must not share identical first keys, which is a bit annoying.
Based on the documentation and the list of unsupported keywords, here are the common and useful JSON Schema keywords that apply to each data type, which are not mentioned as unsupported and are commonly seen in JSON Schema:
Thanks for the insights!
default: Specifies a default value for the data
Although it’s not explicitly mentioned on the docs, trying setting default values using a Pydantic spec returns 400 error.
See an example on from this nice article (link below):
class Article(BaseModel):
title: str
author: str | None = None
text: str
client.beta.chat.completions.parse(
model="gpt-4o-2024-08-06",
messages=[
{"role": "system", "content": "You are a helpful assistant. Extract the provided article."},
{"role": "user", "content": article_text}
response_format=Article
Returns an openai.BadRequestError:
'error': {
'message': "Invalid schema for response_format 'Article': In context=('properties', 'author'), 'default' is not permitted",
'type': 'invalid_request_error',
'param': 'response_format',
'code': None