Skip to content

Schema validation error #2533

@DamyanBG

Description

@DamyanBG

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

  • This is an issue with the Python library

Describe the bug

Hello,

I can see something interesting. I have Pydantic model:

class ReviewSentiment(BaseModel):
    sentiment: str
    tags: List[str]
    aspect_sentiment: Dict[str, str]

When I am trying that code:

prompt = f"""
    Analyze the following product review and return a JSON object with these fields:
    - sentiment: Overall sentiment as one of 'very positive', 'positive', 'neutral', 'negative', 'very negative'.
    - tags: List of up to 3 main topics or issues (e.g., ['shipping', 'quality', 'customer_service']).
    -     aspect_sentiment: dict[str, str]  # Maps aspect (like 'shipping', 'quality') to its sentiment (e.g., {{'shipping': 'positive', 'quality': 'negative'}}).

    Review rating (1-5): {review.rating}
    Review title: {review.title}
    Review content: {review.content}
    Return JSON only.
    """
    client = OpenAI()

    response = client.responses.parse(
        model="gpt-4o",
        input=[
            {
                "role": "system",
                "content": prompt,
            },
        ],
        text_format=ReviewSentiment,
    )

    review_sentiment = response.output_parsed
    print(review_sentiment)

I am receiveing error:

\.venv\Lib\site-packages\openai\_base_client.py", line 1052, in request
    raise self._make_status_error_from_response(err.response) from None
openai.BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'ReviewSentiment': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Extra required key 'aspect_sentiment' supplied.", 'type': 'invalid_request_error', 'param': 'text.format.schema', 'code': 'invalid_json_schema'}}

I actually debugged the code a little bit and I can see the dict schema, which then openai SDK creates:

{
    "input": [
        {
            "role": "system",
            "content": "\n    Analyze the following product review and return a JSON object with these fields:\n    - sentiment: Overall sentiment as one of 'very positive', 'positive', 'neutral', 'negative', 'very negative'.\n    - tags: List of up to 3 main topics or issues (e.g., ['shipping', 'quality', 'customer_service']).\n    -     aspect_sentiment: dict[str, str]  # Maps aspect (like 'shipping', 'quality') to its sentiment (e.g., {'shipping': 'positive', 'quality': 'negative'}).\n\n    Review rating (1-5): 5\n    Review title: Very good\n    Review content: I luked the product very much, delivery was fast\n    Return JSON only.\n    ",
        }
    ],
    "model": "gpt-4o",
    "text": {
        "format": {
            "type": "json_schema",
            "strict": True,
            "name": "ReviewSentiment",
            "schema": {
                "properties": {
                    "sentiment": {"title": "Sentiment", "type": "string"},
                    "tags": {
                        "items": {"type": "string"},
                        "title": "Tags",
                        "type": "array",
                    },
                    "aspect_sentiment": {
                        "additionalProperties": {"type": "string"},
                        "title": "Aspect Sentiment",
                        "type": "object",
                    },
                },
                "required": ["sentiment", "tags", "aspect_sentiment"],
                "title": "ReviewSentiment",
                "type": "object",
                "additionalProperties": False,
            },
        }
    },
}

Everything looks okay, but as I mentioned - I receive an error.

When I tweak a little but the Pydantic model, I do not have error:

class ReviewSentiment(BaseModel):
    sentiment: str
    tags: List[str]
    aspect_sentiment: Dict[str, str] | None = None

And the request:

{
    "input": [
        {
            "role": "system",
            "content": "\\n    Analyze the following product review and return a JSON object with these fields:\\n    - sentiment: Overall sentiment as one of 'very positive', 'positive', 'neutral', 'negative', 'very negative'.\\n    - tags: List of up to 3 main topics or issues (e.g., ['shipping', 'quality', 'customer_service']).\\n    -     aspect_sentiment: dict[str, str]  # Maps aspect (like 'shipping', 'quality') to its sentiment (e.g., {'shipping': 'positive', 'quality': 'negative'}).\\n\\n    Review rating (1-5): 5\\n    Review title: Very good\\n    Review content: I luked the product very much, delivery was fast\\n    Return JSON only.\\n    "
        }
    ],
    "model": "gpt-4o",
    "text": {
        "format": {
            "type": "json_schema",
            "strict": true,
            "name": "ReviewSentiment",
            "schema": {
                "properties": {
                    "sentiment": { "title": "Sentiment", "type": "string" },
                    "tags": { "items": { "type": "string" }, "title": "Tags", "type": "array" },
                    "aspect_sentiment": {
                        "anyOf": [{ "additionalProperties": { "type": "string" }, "type": "object" }, { "type": "null" }],
                        "title": "Aspect Sentiment"
                    }
                },
                "required": ["sentiment", "tags", "aspect_sentiment"],
                "title": "ReviewSentiment",
                "type": "object",
                "additionalProperties": false
            }
        }
    }
}

So that looks very strange at all. I think some error is in the validation in the API, but would like to clarify with you.

To Reproduce

  1. Take the Pydantic model, which I provided
  2. Make a call like in the coded, which I provided
  3. Error occurs

Code snippets

OS

Windows 11

Python version

Python 3.13.5

Library version

openai 1.98.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions