datamodel-code-generator
datamodel-code-generator copied to clipboard
Pydantic Undefined Annotation: name 'Optional' is not defined
Describe the bug
An import statement is missing in the generated model code: you need to ensure that List, Optional, etc are imported from typing. datamodel-code-generator==0.28.3
self = <pydantic._internal._generate_schema.GenerateSchema object at 0x10e7431f0>, name = 'parameters', field_info = FieldInfo(annotation=ForwardRef('Optional[Parameters]'), required=False, default=None)
decorators = DecoratorInfos(validators={}, field_validators={}, root_validators={}, field_serializers={}, model_serializers={}, model_validators={}, computed_fields={})
def _common_field_schema( # C901
self, name: str, field_info: FieldInfo, decorators: DecoratorInfos
) -> _CommonField:
# Update FieldInfo annotation if appropriate:
FieldInfo = import_cached_field_info()
if not field_info.evaluated:
# TODO Can we use field_info.apply_typevars_map here?
try:
evaluated_type = _typing_extra.eval_type(field_info.annotation, *self._types_namespace)
except NameError as e:
> raise PydanticUndefinedAnnotation.from_name_error(e) from e
E pydantic.errors.PydanticUndefinedAnnotation: name 'Optional' is not defined
E
E For further information visit https://errors.pydantic.dev/2.10/u/undefined-annotation
../.venv/lib/python3.10/site-packages/pydantic/_internal/_generate_schema.py:1227: PydanticUndefinedAnnotation
To Reproduce
Example schema:
{
"openapi": "3.1.0",
"info": {
"title": "Get Headers",
"description": "Get headers endpoint",
"version": "v1.0.0"
},
"servers": [
{
"url": "https://{domain}-gntxktyfsq-uc.a.run.app/"
}
],
"paths": {
"/": {
"get": {
"description": "This is a test function to return all request headers.",
"operationId": "PrintHeaders",
"parameters": [
{
"name": "query",
"in": "query",
"description": "Query test parameter. Should be 'test'",
"required": true,
"schema": {
"type": "string"
},
"example": "test"
},
{
"name": "domain",
"in": "path",
"description": "Domain parameter. Should be 'print-headers'",
"required": true,
"schema": {
"type": "string"
},
"example": "print-headers"
}
],
"requestBody": {},
"deprecated": false,
"security": [
{
"apiKey": []
}
]
}
}
},
"components": {
"schemas": {
"RunReportParams": {
"properties": {},
"type": "object",
"required": []
}
},
"securitySchemes": {
"apiKey": {
"type": "apiKey"
}
}
}
}
To reproduce, run tests:
python -m pip install --upgrade pip
pip install -r requirements-dev.txt
pytest -v -k test_tool_factory
Expected behavior Correctly generated model (it worked before - comparison: https://github.com/VRSEN/agency-swarm/compare/main...bonk1t:agency-swarm:fix/observability-and-tests?expand=1).
Version:
- OS: MacOS 15.3.1
- Python version: Python 3.10.
- datamodel-code-generator version: the latest: 0.28.3.
Code that triggers the error: https://github.com/bonk1t/agency-swarm/blob/389bda387eb0e887073c7e1049c16d7c43791433/agency_swarm/tools/ToolFactory.py#L71
A workaround I used: https://github.com/VRSEN/agency-swarm/pull/264/commits/676757c0b95de83828eb1724863464132c4ff58e#diff-6b73d00cdda2ecc6b5b9c34d924f7262e20553ccfefa7b1b5a17314d797be543R107-R150