
    .h                        d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	m
Z
mZ ddlmZmZ ddlmZ dd	lmZ dd
lmZmZ dZ G d de j                  Z ed           G d de                      ZdedefdZdee         defdZdS )    N)	dataclass)Any)	BaseModelTypeAdapter)	TypedDictget_args
get_origin   )ModelBehaviorError	UserError)ensure_strict_json_schema)	SpanError)_error_tracing_jsonresponsec                       e Zd ZdZej        defd            Zej        defd            Z	ej        de
eef         fd            Zej        defd            Zej        dedefd            Zd	S )
AgentOutputSchemaBaseAn object that captures the JSON schema of the output, as well as validating/parsing JSON
    produced by the LLM into the output type.
    returnc                     dS z=Whether the output type is plain text (versus a JSON object).N selfs    P/var/www/FlaskApp/flask-venv/lib/python3.11/site-packages/agents/agent_output.pyis_plain_textz#AgentOutputSchemaBase.is_plain_text   	     	    c                     dS )The name of the output type.Nr   r   s    r   namezAgentOutputSchemaBase.name   r   r   c                     dS )zqReturns the JSON schema of the output. Will only be called if the output type is not
        plain text.
        Nr   r   s    r   json_schemaz!AgentOutputSchemaBase.json_schema   	    
 	r   c                     dS )zWhether the JSON schema is in strict mode. Strict mode constrains the JSON schema
        features, but guarantees valis JSON. See here for details:
        https://platform.openai.com/docs/guides/structured-outputs#supported-schemas
        Nr   r   s    r   is_strict_json_schemaz+AgentOutputSchemaBase.is_strict_json_schema&   s	     	r   json_strc                     dS )zValidate a JSON string against the output type. You must return the validated object,
        or raise a `ModelBehaviorError` if the JSON is invalid.
        Nr   )r   r'   s     r   validate_jsonz#AgentOutputSchemaBase.validate_json.   r$   r   N)__name__
__module____qualname____doc__abcabstractmethodboolr   strr!   dictr   r#   r&   r)   r   r   r   r   r      s         	t     	c     	T#s(^     	t     	c c      r   r   F)initc                       e Zd ZU dZee         ed<   	 ee         ed<   	 eed<   	 e	e
ef         ed<   	 eed<   	 ddee         defd	Zd
efdZd
efdZd
e	e
ef         fdZde
d
efdZd
e
fdZdS )AgentOutputSchemar   output_type_type_adapter_is_wrapped_output_schema_strict_json_schemaTstrict_json_schemac                 h   || _         || _        |	|t          u r;d| _        t	          |          | _        | j                                        | _        dS t          |           | _        | j        rJt          dt          |i          }t	          |          | _        | j                                        | _        n2t	          |          | _        | j                                        | _        | j        r>	 t          | j                  | _        dS # t          $ r}t          d          |d}~ww xY wdS )a	  
        Args:
            output_type: The type of the output.
            strict_json_schema: Whether the JSON schema is in strict mode. We **strongly** recommend
                setting this to True, as it increases the likelihood of correct JSON input.
        NF
OutputTypezStrict JSON schema is enabled, but the output type is not valid. Either make the output type strict, or pass output_schema_strict=False to your Agent())r6   r:   r1   r8   r   r7   r#   r9   "_is_subclass_of_base_model_or_dictr   _WRAPPER_DICT_KEYr   r   )r   r6   r;   r=   es        r   __init__zAgentOutputSchema.__init__O   sW    '#5 +"4"4$D!,[!9!9D"&"4"@"@"B"BDF  B+NNN 	C"%{ J "-Z!8!8D"&"4"@"@"B"BD!,[!9!9D"&"4"@"@"B"BD# 	&?@S&T&T###   #  		 	s   5D 
D/D**D/r   c                 0    | j         du p| j         t          u S r   )r6   r1   r   s    r   r   zAgentOutputSchema.is_plain_textz   s    4'B4+;s+BBr   c                     | j         S )z*Whether the JSON schema is in strict mode.)r:   r   s    r   r&   z'AgentOutputSchema.is_strict_json_schema~   s    ''r   c                 V    |                                  rt          d          | j        S )z#The JSON schema of the output type.z9Output type is plain text, so no JSON schema is available)r   r   r9   r   s    r   r#   zAgentOutputSchema.json_schema   s/     	YWXXX""r   r'   c           
         t          j        || j        d          }| j        rt	          |t
                    sWt          j        t          dddt          |           i                     t          dt          |           d|           t          |vrHt          j        t          dddt           d	i                     t          dt           d
|           |t                   S |S )zValidate a JSON string against the output type. Returns the validated object, or raises
        a `ModelBehaviorError` if the JSON is invalid.
        F)partialzInvalid JSONdetailszExpected a dict, got )messagedataz for JSON: zCould not find key z in JSONz
 in JSON: )r   r)   r7   r8   
isinstancer2   r   attach_error_to_current_spanr   typer   r?   )r   r'   	validateds      r   r)   zAgentOutputSchema.validate_json   s1    '$2DeTTT	 	0i.. 	; .')Ri)R)RS     )RDOORRRR   !	11; .')Z?P)Z)Z)Z[     )Q*;QQxQQ   .//r   c                 *    t          | j                  S )r    )_type_to_strr6   r   s    r   r!   zAgentOutputSchema.name   s    D,---r   N)T)r*   r+   r,   r-   rL   r   __annotations__r   r0   r2   r1   rA   r   r&   r#   r)   r!   r   r   r   r5   r5   6   sK          c!s####R cN"""() )DI )4 ) ) ) )VCt C C C C(t ( ( ( (#T#s(^ # # # #c c    <.c . . . . . .r   r5   tr   c                     t          | t                    sdS t          |           }t          t          f}t          |p| |          S )NF)rJ   rL   r	   r   r2   
issubclass)rQ   originallowed_typess      r   r>   r>      sD    a u ]]F%Mfk=111r   c                     t          |           }t          |           }|| j        S |r,d                    d |D                       }|j         d| dS t	          |           S )Nz, c              3   4   K   | ]}t          |          V  d S )N)rO   ).0args     r   	<genexpr>z_type_to_str.<locals>.<genexpr>   s*      ??3\#..??????r   [])r	   r   r*   joinr1   )rQ   rT   argsargs_strs       r   rO   rO      sq    ]]FA;;D~z	 99??$?????///H////1vvr   )r.   dataclassesr   typingr   pydanticr   r   typing_extensionsr   r   r	   
exceptionsr   r   strict_schemar   tracingr   utilr   r   r?   ABCr   r5   r0   r>   rL   r1   rO   r   r   r   <module>ri      s   



 ! ! ! ! ! !       + + + + + + + + = = = = = = = = = = 5 5 5 5 5 5 5 5 4 4 4 4 4 4       ' ' ' ' ' ' ' ' # # # # #CG # # #L q. q. q. q. q.- q. q. q.h	2# 	2$ 	2 	2 	2 	2DI #      r   