Azure / Azure/azure-sdk-for-python

[Document Intelligence] Compatibility with pydantic

Open
#38,926 2 comments 1 reaction 1 assignee Assigned to @bojunehsu View on GitHub
Client customer-reported Document Intelligence feature-request needs-team-attention Service Attention
Dominant language
Python
Stars
5.6k
Forks
3.4k
Avg merge
1d 21h
Merged PRs (30d)
193

Description

**Is your feature request related to a problem? Please describe.**
I want to be able to use the type `AnalyzeResult` in a pydantic base model. One advantage is that if `AnalyzeResult` is part of the response for a FastAPI endpoint, proper Swagger documentation will be generated.

```python
from datetime import datetime, timezone
from typing import Any

from azure.ai.documentintelligence.models import AnalyzeResult
from pydantic import BaseModel, ConfigDict, Field

def generate_timestamp() -> datetime:
"""Generate timestamp based on UTC timezone"""

return datetime.now(timezone.utc)

class BaseEvent(BaseModel):
"""Base event"""

timestamp: datetime = Field(default_factory=generate_timestamp)

class AnalyzeEndEvent(BaseEvent):
"""End of document analysis"""

analyze_result: AnalyzeResult

# model_config = ConfigDict(arbitrary_types_allowed=True) # can't generate schema for AnalyzeResult
```

Currently `azure-ai-documentintelligence:1.0.0b4`, it causes the following error:
```
Traceback (most recent call last):
File "c:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\document_intelligence_caching\callbacks\events.py", line 26, in
class AnalyzeEndEvent(BaseEvent):
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_model_construction.py", line 226, in __new__
complete_model_class(
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_model_construction.py", line 658, in complete_model_class
schema = cls.__get_pydantic_core_schema__(cls, handler)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\main.py", line 702, in __get_pydantic_core_schema__
return handler(source)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 84, in __call__
schema = self._handler(source_type)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 612,
in generate_schema
schema = self._generate_schema_inner(obj)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 881,
in _generate_schema_inner
return self._model_schema(obj)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 693,
in _model_schema
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 693,
in
{k: self._generate_md_field_schema(k, v, decorators) for k, v in fields.items()},
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 1073, in _generate_md_field_schema
common_field = self._common_field_schema(name, field_info, decorators)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 1265, in _common_field_schema
schema = self._apply_annotations(
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 2062, in _apply_annotations
schema = get_inner_schema(source_type)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_schema_generation_shared.py", line 84, in __call__
schema = self._handler(source_type)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 2043, in inner_handler
schema = self._generate_schema_inner(obj)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 886,
in _generate_schema_inner
return self.match_type(obj)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 997,
in match_type
return self._unknown_type_schema(obj)
File "C:\Users\_\Desktop\Repos\Package-DocumentIntelligenceCaching\.conda\lib\site-packages\pydantic\_internal\_generate_schema.py", line 515,
in _unknown_type_schema
raise PydanticSchemaGenerationError(
pydantic.errors.PydanticSchemaGenerationError: Unable to generate pydantic-core schema for . Set `arbitrary_types_allowed=True` in the model_config to ignore this error or implement `__get_pydantic_core_schema__` on your type to fully support it.

If you got this error by calling handler() within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema()` since we do not call `__get_pydantic_core_schema__` on `` otherwise to avoid infinite recursion.

For further information visit https://errors.pydantic.dev/2.10/u/schema-for-unknown-type
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.