
    =i7                    D   S r SSKJ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        r/home/dmtnaga/Documents/work/airagagent/rag_env/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   L   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)_SchemaSpech   z%Describes a structured output schema.type[SchemaT]rH   rB   namedescription
SchemaKindrI   dict[str, Any]r   FboolstrictN)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)z:Initialize SchemaSpec with schema and optional parameters.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   h   s    / I 
 1FD=  "&+"+" 	+"
  +" +" 
+" +"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.rT   rH   zlist[_SchemaSpec[SchemaT]]schema_specsri   tool_message_contentWbool | str | type[Exception] | tuple[type[Exception], ...] | Callable[[Exception], str]handle_errorsNT)rn   rp   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      [        [        4;   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)   rv   -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   rn   rp   rR   rm   )r2   rH   rn   rp   srv   s        @r)   r0   ToolStrategy.__init__   sC      $8!*	 6DF5KL5K[^5KLLs   A)rp   rH   rm   rn   )rH   rT   rn   ri   rp   ro   r5   r6   r!   r"   r#   r$   r%   r&   r0   r'   r    r(   r)   rk   rk      sr    :$,,*$$- 	`	 ,0
 (,"M"M )	"M
%"M 
"M "Mr(   rk   c                  N    \ rS rSr% SrS\S'    S\S'        SS jrSS jrS	rg
)ProviderStrategy   z9Use the model provider's native structured output method.rT   rH   _SchemaSpec[SchemaT]schema_specc                0    Xl         [        U5      U l        g)z(Initialize ProviderStrategy with schema.N)rH   rR   r   r2   rH   s     r)   r0   ProviderStrategy.__init__   s    
 &v.r(   c                j    SU R                   R                  U R                   R                  S.S.nSU0$ )z@Convert to kwargs to bind to a model to force structured output.r   )rU   rH   )re   r   response_format)r   rU   r   )r2   r   s     r)   to_model_kwargs ProviderStrategy.to_model_kwargs  s>    
 "((--**66
 "?33r(   )rH   r   NrH   rT   r5   r6   )r5   rX   )	r!   r"   r#   r$   r%   r&   r0   r   r'   r    r(   r)   r|   r|      s3    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)OutputToolBindingi  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_spec(  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     s@     D M
NL
 
&Lr(   r   c                  Z    \ rS rSr% SrS\S'    S\S'    \SS j5       rSS jrSS	 jr	S
r
g)ProviderStrategyBindingiK  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SSKn U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   Nr!   r   z1Native structured output expected valid JSON for z, but parsing failed: r>   )	"_extract_text_content_from_messagejsonloadsrC   rF   rH   rG   rO   rI   )r2   responseraw_textr   rJ   rL   rM   rN   s           r)   r   ProviderStrategyBinding.parsej  s     ::8D	)::h'D "$++t/?/?FF  	)!$++z;LMKCK= Q''(c,  S/q(	)s   A	 	
B,A??Bc                   UR                   n[        U[        5      (       a  U$ [        U[        5      (       a  / 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      $ [        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   listra   rb   appendr1   )r2   messager   partscs        r)   r   :ProviderStrategyBinding._extract_text_content_from_message  s     //gs##Ngt$$!Ea&&uuV}.6Q;S6^4"aJq|S,I,IQy\2 -J LLQ(  775>!7|r(   r    Nr   )r   r   r5   r   )r   r   r5   rB   )r!   r"   r#   r$   r%   r&   r   r   r   r   r'   r    r(   r)   r   r   K  s=     H M
 
G<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   r   s     r)   r0   AutoStrategy.__init__  s	    
 r(   r   Nr   rz   r    r(   r)   r   r     s%    G$ 
r(   r   )rH   ztype[SchemaT] | dictrI   rW   rJ   rX   r5   r   ).r%   
__future__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"% %/9%AO%%8 H"''" H" H"V =M77# =M =M@ 4ww' 4 4> 2L( 2L 2Lj Sgg. S Sl77#  g&)9')BB\RYEZZr(   