Skip to content

Generate OpenAI OpenAPI for python pydantic2.datamodel #2972

@guenhter

Description

@guenhter

Describe the bug

OpenAI models cannot be properly generated. I mean they are generated, but then pydantic errors are raised.

Issue for discussion #2965

To Reproduce

OPENAPI_URL="https://app.stainless.com/api/spec/documented/openai/openapi.documented.yml"
OPENAPI_FILE="./openapi.documented.yml"

echo "Downloading OpenAPI spec from $OPENAPI_URL..."
curl -s --fail-with-body -o "$OPENAPI_FILE" "$OPENAPI_URL"

# Generate Python models using datamodel-code-generator
OUTPUT_FILE="./openapi_models.py"

echo "Generating Python models..."

# Run datamodel-code-generator with basic options similar to the main models
uv run datamodel-codegen \
    --input "$OPENAPI_FILE" \
    --output "$OUTPUT_FILE" \
    --output-model-type pydantic_v2.BaseModel \
    --formatters ruff-format \
    --input-file-type openapi

Then the models are generated, but when using them, I get this error:

_schema
    raise PydanticUserError(
E   pydantic.errors.PydanticUserError: Model 'FineTuningIntegration' needs field 'type' to be of type `Literal`
E   
E   For further information visit https://errors.pydantic.dev/2.12/u/discriminator-needs-literal

Expected behavior
Models are generated and usable

Version:

  • OS: Ubuntu 24.04
  • Python version: 3.13
  • datamodel-code-generator version: v.0.53.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions