添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
失恋的滑板  ·  springmvc配置MappingJack ...·  11 小时前    · 
帅气的弓箭  ·  Jackson·  11 小时前    · 
怕老婆的卤蛋  ·  C# 基本语法 | ·  6 小时前    · 
沉着的红烧肉  ·  SHOW PROCESSLIST | ...·  4 月前    · 
眉毛粗的豆浆  ·  docker-compose up ...·  7 月前    · 
咆哮的馒头  ·  Combating fake news: ...·  10 月前    · 

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?

Your option is anyOf, to specify sub-schema, but it cannot be the root.

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:

String

  • type: Specifies that the data should be a string.
  • enum: Restricts the value to a fixed set of string values.
  • const: Requires the string to be exactly equal to a specified value.
  • contentEncoding: (If supported) Specifies the content encoding (e.g., base64).
  • contentMediaType: (If supported) Specifies the media type of the string contents (e.g., text/html).
  • Note: The keywords minLength, maxLength, pattern, and format are explicitly unsupported.

    Number and Integer

  • type: Specifies that the data should be a number or integer.
  • enum: Restricts the value to a fixed set of numerical values.
  • const: Requires the number to be exactly equal to a specified value.
  • exclusiveMinimum: Requires the number to be greater than a specified value.
  • exclusiveMaximum: Requires the number to be less than a specified value.
  • Note: The keywords minimum, maximum, and multipleOf are explicitly unsupported.

    Boolean

  • type: Specifies that the data should be a boolean.
  • enum: Can restrict the value to true or false.
  • const: Requires the value to be exactly true or false.
  • Object

  • type: Specifies that the data should be an object.
  • properties: Defines the properties and their respective schemas.
  • required: Lists the properties that must be present in the object.
  • additionalProperties: Controls whether properties not listed in properties are allowed.
  • dependencies: (If supported) Specifies property dependencies within the object.
  • patternDependentSchemas: (If supported) Applies schemas based on property patterns.
  • Note: The keywords patternProperties, unevaluatedProperties, propertyNames, minProperties, and maxProperties are explicitly unsupported.

    Array

  • type: Specifies that the data should be an array.
  • items: Defines the schema for items within the array.
  • additionalItems: Controls whether additional items are allowed beyond those specified in items.
  • prefixItems: (If supported) Defines schemas for tuple validation.
  • Note: The keywords unevaluatedItems, contains, minContains, maxContains, minItems, maxItems, and uniqueItems are explicitly unsupported.

    General Keywords Applicable to Multiple Data Types

  • enum: (supported) Restricts the value to a fixed set of values.
  • const: (no effect on functions) Requires the value to be exactly equal to a specified value.
  • anyOf: Requires the data to match at least one of the specified schemas.
  • allOf: (not supported) Requires the data to match all of the specified schemas.
  • oneOf: (not supported) Requires the data to match exactly one of the specified schemas.
  • not: (If supported) Requires the data to not match the specified schema.
  • if, then, else: (If supported) Conditional subschemas based on the data.
  • description: Provides a description for documentation purposes.
  • title: (supported, prefix to description) Provides a short title for the schema or property.
  • default: (not supported) Specifies a default value for the data.
  • Additional Notes

    Schema References only internal, obviously, but yes:

  • $ref: (no) References a schema defined elsewhere, allowing for schema reuse.
  • $defs or definitions: (yes) Allows for defining reusable schemas within the current schema.
  • Combining Schemas only allOf is even mentioned in dox:

  • allOf, oneOf, not: Useful for composing complex schemas from simpler ones. Since they are not listed as unsupported, they might still be supported.
  • Conditional Subschemas unknown, unlikely:

  • if, then, else: Enable conditional validation based on the value of the data.
  • Annotations unknown:

  • description and title: Useful for providing human-readable information about the schema, which can be important for documentation and tooling.
  • Hi @diego.delarue and welcome to the community!

    Also relevant for @admin215 !

    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

    Any ideas on why?

    Useful link: