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

[Document Intelligence] Compatibility with pydantic #38926

Open
ai-learner-00 opened this issue Dec 17, 2024 · 0 comments
Open

[Document Intelligence] Compatibility with pydantic #38926

ai-learner-00 opened this issue Dec 17, 2024 · 0 comments
Assignees
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Document Intelligence question The issue doesn't require a change to the product in order to be resolved. Most issues start as that

Comments

@ai-learner-00
Copy link

ai-learner-00 commented Dec 17, 2024

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
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Dec 17, 2024
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issues that are reported by GitHub users external to the Azure organization. Document Intelligence question The issue doesn't require a change to the product in order to be resolved. Most issues start as that
Projects
None yet
Development

No branches or pull requests

3 participants