Skip to content

[Document Intelligence] Compatibility with pydantic #38926

Open
@ai-learner-00

Description

@ai-learner-00

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.

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 <module>
    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 <dictcomp>
    {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 <class 'azure.ai.documentintelligence.models._models.AnalyzeResult'>. 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(<some type>) within `__get_pydantic_core_schema__` then you likely need to call `handler.generate_schema(<some type>)` since we do not call `__get_pydantic_core_schema__` on `<some type>` otherwise to avoid infinite recursion.

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

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.Document IntelligenceService AttentionWorkflow: This issue is responsible by Azure service team.customer-reportedIssues that are reported by GitHub users external to the Azure organization.feature-requestThis issue requires a new behavior in the product in order be resolved.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK team

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions