
    OKi;                    L   S r SSKJr  SSKrSSKrSSKJrJr  SSKJ	r	  SSK
JrJrJrJrJrJrJrJr  SSKJrJr  SSKJrJr  SS	KJrJr  \(       a  SS
KJrJr  SSKJ r   \" S5      r!\S   r" " S S\#5      r$ " S S\$5      r% " S S\$5      r&        S#S jr'\" SS9 " S S\\!   5      5       r(\" SS9 " S S\\!   5      5       r)\" SS9 " S S\\!   5      5       r*\ " S S\\!   5      5       r+\ " S S \\!   5      5       r, " S! S"\\!   5      r-\)\!   \*\!   -  \-\!   -  r.g)$z)Types for setting agent response formats.    )annotationsN)	dataclassis_dataclass)	UnionType)TYPE_CHECKINGAnyGenericLiteralTypeVarUnionget_args
get_origin)BaseToolStructuredTool)	BaseModelTypeAdapter)Selfis_typeddict)CallableIterable)	AIMessageSchemaT)pydanticr   	typeddictjson_schemac                  $    \ rS rSr% SrS\S'   Srg)StructuredOutputError#   z(Base class for structured output errors.r   
ai_message N)__name__
__module____qualname____firstlineno____doc____annotations____static_attributes__r        d/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/langchain/agents/structured_output.pyr   r   #   s    2r(   r   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )MultipleStructuredOutputsError)   zZRaised when model returns multiple structured output tool calls when only one is expected.c                b   > Xl         X l        [        TU ]  SSR	                  U5       S35        g)zInitialize `MultipleStructuredOutputsError`.

Args:
    tool_names: The names of the tools called for structured output.
    ai_message: The AI message that contained the invalid multiple tool calls.
z:Model incorrectly returned multiple structured responses (z, z) when only one is expected.N)
tool_namesr   super__init__join)selfr.   r   	__class__s      r)   r0   'MultipleStructuredOutputsError.__init__,   s9     %$		*%&&BD	
r(   )r   r.   )r.   z	list[str]r   r   returnNoner!   r"   r#   r$   r%   r0   r'   __classcell__r3   s   @r)   r+   r+   )   s    d
 
r(   r+   c                  0   ^  \ rS rSrSrSU 4S jjrSrU =r$ )StructuredOutputValidationError<   zXRaised when structured output tool call arguments fail to parse according to the schema.c                V   > Xl         X l        X0l        [        TU ]  SU SU S35        g)zInitialize `StructuredOutputValidationError`.

Args:
    tool_name: The name of the tool that failed.
    source: The exception that occurred.
    ai_message: The AI message that contained the invalid structured output.
z,Failed to parse structured output for tool 'z': .N)	tool_namesourcer   r/   r0   )r2   r?   r@   r   r3   s       r)   r0   (StructuredOutputValidationError.__init__?   s5     #$G	{RUV\U]]^_`r(   )r   r@   r?   )r?   strr@   	Exceptionr   r   r5   r6   r7   r9   s   @r)   r;   r;   <   s    ba ar(   r;   c                    US:X  a  U$  [        U 5      nUR                  U5      $ ! [         a/  n[        U S[	        U 5      5      nSU SU 3n[        U5      UeSnAff = f)ae  Parse data using for any supported schema type.

Args:
    schema: The schema type (Pydantic model, `dataclass`, or `TypedDict`)
    schema_kind: One of `'pydantic'`, `'dataclass'`, `'typeddict'`, or
        `'json_schema'`
    data: The data to parse

Returns:
    The parsed instance according to the schema type

Raises:
    ValueError: If parsing fails
r   r!   zFailed to parse data to z: N)r   validate_pythonrC   getattrrB   
ValueError)schemaschema_kinddataadaptereschema_namemsgs          r)   _parse_with_schemarO   M   sq    " m#%(3F(;&&t,, %fj#f+>(Rs;o1$%s   & 
A*AAF)initc                      \ rS rSr% SrS\S'    S\S'    S\S'    S\S	'    S
\S'    SrS\S'    SSSS.         SS jjrSrg)_SchemaSpeci   z%Describes a structured output schema.type[SchemaT] | dict[str, Any]rH   rB   namedescription
SchemaKindrI   dict[str, Any]r   Nbool | Nonestrict)rU   rV   rZ   c                  Xl         U(       a  X l        O[        U[        5      (       aC  [	        UR                  SS[	        [        R                  " 5       5      SS  35      5      U l        O=[	        [        USS[	        [        R                  " 5       5      SS  35      5      U l        U=(       d=    [        U[        5      (       a  UR                  SS5      O[        USS5      =(       d    SU l	        X@l
        [        U[        5      (       a  S	U l        Xl        g[        U[        5      (       a2  [        U[        5      (       a  S
U l        UR!                  5       U l        g[#        U5      (       a&  SU l        [%        U5      R                  5       U l        g['        U5      (       a&  SU l        [%        U5      R                  5       U l        gS[        U5       S3n[)        U5      e)a>  Initialize `SchemaSpec` with schema and optional parameters.

Args:
    schema: Schema to describe.
    name: Optional name for the schema.
    description: Optional description for the schema.
    strict: Whether to enforce strict validation of the schema.

Raises:
    ValueError: If the schema type is unsupported.
titleresponse_format_N   r!   rV    r%   r   r   r   r   zUnsupported schema type: zS. Supported types: Pydantic models, dataclasses, TypedDicts, and JSON schema dicts.)rH   rU   
isinstancedictrB   getuuiduuid4rF   rV   rZ   rI   r   type
issubclassr   model_json_schemar   r   r   rG   )r2   rH   rU   rV   rZ   rN   s         r)   r0   _SchemaSpec.__init__   s   & I%%FJJw2B3tzz|CTUWVWCXBY0Z[\DIGFJ:J3tzz|K\]_^_K`Ja8bcdDI& 
&$'' JJ}b)D17R 	 fd##,D%%%*VY*G*G)D%779D&!!*D*62>>@D&!!*D*62>>@D ,DL> :d e  S/!r(   )rV   r   rU   rH   rI   rZ   )
rH   rT   rU   
str | NonerV   ri   rZ   rY   r5   r6   )	r!   r"   r#   r$   r%   r&   rZ   r0   r'   r    r(   r)   rR   rR   i   s    /** I 
 1FK=  "&"5".5" 	5"
  5" 5" 
5" 5"r(   rR   c                  l    \ rS rSr% SrS\S'    S\S'    S\S'    S	\S
'    SSS.       SS jjrSrg)ToolStrategy   z0Use a tool calling strategy for model responses.*type[SchemaT] | UnionType | dict[str, Any]rH   zlist[_SchemaSpec[Any]]schema_specsri   tool_message_contentWbool | str | type[Exception] | tuple[type[Exception], ...] | Callable[[Exception], str]handle_errorsNT)ro   rq   c                  ^ Xl         X l        X0l        SU4S jjmT" U5       Vs/ s H  n[        U5      PM     snU l        gs  snf )zwInitialize `ToolStrategy`.

Initialize `ToolStrategy` with schemas, tool message content, and error handling
strategy.
c              3     >#    [        U 5      [        [        1;   a#  [        U 5       H  nT" U5       Sh  vN   M     g[	        U [
        5      (       a0  SU ;   a*  U R                  S/ 5       H  nT" U5       Sh  vN   M     gU v   g NS N7f)z5Yield leaf variants from Union and JSON Schema oneOf.NoneOf)r   r   r   r   r`   ra   rb   )rH   argsub_iter_variantss      r)   rw   -ToolStrategy.__init__.<locals>._iter_variants   s     &!i%77#F+C-c222 ,&$''Gv,=!::gr2C-c222 3L 3
 3s"   3BB
AB;B<BBN)rH   r   r5   zIterable[Any])rH   ro   rq   rR   rn   )r2   rH   ro   rq   srw   s        @r)   r0   ToolStrategy.__init__   sC      $8!*	 6DF5KL5K[^5KLLs   A)rq   rH   rn   ro   )rH   rm   ro   ri   rq   rp   r5   r6   r!   r"   r#   r$   r%   r&   r0   r'   r    r(   r)   rk   rk      sr    :66$((*$$
 	`	 ,0
 (,"M:"M )	"M
%"M 
"M "Mr(   rk   c                  X    \ rS rSr% SrS\S'    S\S'    SS.     SS	 jjrSS
 jrSrg)ProviderStrategyi  z9Use the model provider's native structured output method.rT   rH   _SchemaSpec[SchemaT]schema_specNrZ   c               ,    Xl         [        XS9U l        g)zInitialize `ProviderStrategy` with schema.

Args:
    schema: Schema to enforce via the provider's native structured output.
    strict: Whether to request strict provider-side schema enforcement.
r   N)rH   rR   r   )r2   rH   rZ   s      r)   r0   ProviderStrategy.__init__  s     &v=r(   c                    U R                   R                  U R                   R                  S.nU R                   R                  (       a  SUS'   SUS.nSU0$ )znConvert to kwargs to bind to a model to force structured output.

Returns:
    The kwargs to bind to a model.
)rU   rH   TrZ   r   )re   r   response_format)r   rU   r   rZ   )r2   r   r   s      r)   to_model_kwargs ProviderStrategy.to_model_kwargs  s`     $$))&&22'
 ""$(K! "&+
 "?33r(   )rH   r   )rH   rT   rZ   rY   r5   r6   )r5   rX   )	r!   r"   r#   r$   r%   r&   r0   r   r'   r    r(   r)   r}   r}     sB    C**!%%& #	>.> 	>
 
>4r(   r}   c                  \    \ rS rSr% SrS\S'    S\S'    S\S'    \SS	 j5       rSS
 jrSr	g)OutputToolBindingi3  a  Information for tracking structured output tool metadata.

This contains all necessary information to handle structured responses generated via
tool calls, including the original schema, its type classification, and the
corresponding tool implementation used by the tools strategy.
rT   rH   rW   rI   r   toolc           
         U " UR                   UR                  [        UR                  UR                  UR
                  S9S9$ )zCreate an `OutputToolBinding` instance from a `SchemaSpec`.

Args:
    schema_spec: The `SchemaSpec` to convert

Returns:
    An `OutputToolBinding` instance with the appropriate tool created
)args_schemarU   rV   )rH   rI   r   )rH   rI   r   r   rU   rV   clsr   s     r)   from_schema_spec"OutputToolBinding.from_schema_specG  sF     %%#//'33 %%'33
 	
r(   c                D    [        U R                  U R                  U5      $ )zParse tool arguments according to the schema.

Args:
    tool_args: The arguments from the tool call

Returns:
    The parsed response according to the schema type

Raises:
    ValueError: If parsing fails
)rO   rH   rI   )r2   	tool_argss     r)   parseOutputToolBinding.parse[  s     "$++t/?/?KKr(   r    Nr   r~   r5   r   )r   rX   r5   r   )
r!   r"   r#   r$   r%   r&   classmethodr   r   r'   r    r(   r)   r   r   3  s?     +* M
NL
 
&Lr(   r   c                  d    \ rS rSr% SrS\S'    S\S'    \SS j5       rSS jr\	SS	 j5       r
S
rg)ProviderStrategyBindingij  a  Information for tracking native structured output metadata.

This contains all necessary information to handle structured responses generated via
native provider output, including the original schema, its type classification, and
parsing logic for provider-enforced JSON.
rT   rH   rW   rI   c                8    U " UR                   UR                  S9$ )zCreate a `ProviderStrategyBinding` instance from a `SchemaSpec`.

Args:
    schema_spec: The `SchemaSpec` to convert

Returns:
    A `ProviderStrategyBinding` instance for parsing native structured output
rH   rI   r   r   s     r)   r   (ProviderStrategyBinding.from_schema_spec{  s#     %%#//
 	
r(   c                   U R                  U5      n [        R                  " U5      n[        U R
                  U R                  U5      $ ! [         a1  n[	        U R
                  SS5      nSU SU S3n[        U5      UeSnAff = f)a  Parse `AIMessage` content according to the schema.

Args:
    response: The `AIMessage` containing the structured output

Returns:
    The parsed response according to the schema

Raises:
    ValueError: If text extraction, JSON parsing or schema validation fails
r!   r   z1Native structured output expected valid JSON for z, but parsing failed: r>   N)	"_extract_text_content_from_messagejsonloadsrC   rF   rH   rG   rO   rI   )r2   responseraw_textrJ   rL   rM   rN   s          r)   r   ProviderStrategyBinding.parse  s     ::8D	)::h'D "$++t/?/?FF  	)!$++z;LMKCK= Q''(c,  S/q(	)s   A
 

B,B  Bc                   U R                   n[        U[        5      (       a  U$ / nU H  n[        U[        5      (       ar  UR	                  S5      S:X  a%  SU;   a  UR                  [        US   5      5        MR  SU;   a0  [        US   [        5      (       a  UR                  US   5        M  M  M  UR                  [        U5      5        M     SR                  U5      $ )zExtract text content from an `AIMessage`.

Args:
    message: The AI message to extract text from

Returns:
    The extracted text content
re   textcontentr_   )r   r`   rB   ra   rb   appendr1   )messager   partscs       r)   r   :ProviderStrategyBinding._extract_text_content_from_message  s     //gs##NA!T""55=F*v{LLQvY0!^
1Y<(E(ELL9. )F^ SV$  wwu~r(   r    Nr   )r   r   r5   r   )r   r   r5   rB   )r!   r"   r#   r$   r%   r&   r   r   r   staticmethodr   r'   r    r(   r)   r   r   j  sK     +* M
 
G8  r(   r   c                  8    \ rS rSr% SrS\S'        SS jrSrg)	AutoStrategyi  z=Automatically select the best strategy for structured output.rT   rH   c                    Xl         g)z&Initialize `AutoStrategy` with schema.NrH   )r2   rH   s     r)   r0   AutoStrategy.__init__  s	    
 r(   r   N)rH   rT   r5   r6   r{   r    r(   r)   r   r     s%    G**$. 
r(   r   )rH   rT   rI   rW   rJ   rX   r5   r   )/r%   
__future__r   r   rc   dataclassesr   r   typesr   typingr   r   r	   r
   r   r   r   r   langchain_core.toolsr   r   r   r   r   typing_extensionsr   r   collections.abcr   r   langchain_core.messagesr   r   rW   rC   r   r+   r;   rO   rR   rk   r}   r   r   r   ResponseFormatr    r(   r)   <module>r      s   / "   / 	 	 	 : + 021 )
HI
I 
%: 
&a&; a"%*%9C%KY%%8 U"''" U" U"p >M77# >M >MB +4ww' +4 +4\ 3L( 3L 3Ll Qgg. Q Qh77#  g&)9')BB\RYEZZ >r(   