Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect recognition of Literal value #2178

Open
steadyfirmness opened this issue Nov 21, 2024 · 0 comments
Open

Incorrect recognition of Literal value #2178

steadyfirmness opened this issue Nov 21, 2024 · 0 comments

Comments

@steadyfirmness
Copy link

Describe the bug
Often the name is used as the value of Literal

To Reproduce

Example schema:

openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/openai-aa9b01fc0c17eb0cbc200533fc20d6a49c5e764ceaf8049e08b294532be6e9ff.yml

Used commandline:

$  datamodel-codegen \
 --input openai-aa9b01fc0c17eb0cbc200533fc20d6a49c5e764ceaf8049e08b294532be6e9ff.yml \
 --output models.py \
 --output-model-type pydantic_v2.BaseModel \
 --target-python-version 3.12 \
 --enum-field-as-literal all \
 --field-constraints \
 --use-standard-collections \
 --use-union-operator \
 --field-include-all-keys \
 --use-default-kwarg \
 --use-exact-imports \
 --use-schema-description \
 --treat-dot-as-module \
 --use-title-as-name \

Expected behavior

(part of yml)
OtherChunkingStrategyResponseParam:
      type: object
      title: Other Chunking Strategy
      description: >-
        This is returned when the chunking strategy is unknown. Typically, this is because the file was
        indexed before the `chunking_strategy` concept was introduced in the API.
      additionalProperties: false
      properties:
        type:
          type: string
          description: Always `other`.
          enum:
            - other
      required:
        - type
(what i expected)
class OtherChunkingStrategy(BaseModel):
    """
    This is returned when the chunking strategy is unknown. Typically, this is because the file was indexed before the `chunking_strategy` concept was introduced in the API.
    """

    model_config = ConfigDict(
        extra='forbid',
    )
    type: Literal['other'] = Field(
        ..., description='Always `other`.'
    )
(What actually happened)
class OtherChunkingStrategy(BaseModel):
    """
    This is returned when the chunking strategy is unknown. Typically, this is because the file was indexed before the `chunking_strategy` concept was introduced in the API.
    """

    model_config = ConfigDict(
        extra='forbid',
    )
    type: Literal['OtherChunkingStrategyResponseParam'] = Field(
        ..., description='Always `other`.'
    )

Noted that the 'Literal['OtherChunkingStrategyResponseParam']' instead of Literal['other']

Version:

  • OS: macis
  • Python version: 3.12
  • datamodel-code-generator version: 0.26.0

Additional context
In this case, the frequency of occurrence of only one enumeration value is high。
e.g

class CodeInterpreterImageOutput(BaseModel):
    index: int = Field(..., description='The index of the output in the outputs array.')
    type: Literal['RunStepDeltaStepDetailsToolCallsCodeOutputImageObject'] = Field(
        ..., description='Always `image`.'
    )
    image: Image1 | None = None


class CodeInterpreterLogOutput(BaseModel):
    """
    Text output from the Code Interpreter tool call as part of a run step.
    """

    index: int = Field(..., description='The index of the output in the outputs array.')
    type: Literal['RunStepDeltaStepDetailsToolCallsCodeOutputLogsObject'] = Field(
        ..., description='Always `logs`.'
    )
    logs: str | None = Field(
        default=None, description='The text output from the Code Interpreter tool call.'
    )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant