
    3fi                      U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZmZmZmZm Z m!Z! ddl"m#Z# ddl$Z$ddl%Z%ddl&Z&ddl'm(Z(m)Z) ddl*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA ddl0mBZC ddlDmEZEmFZFmGZG ddlHmIZImJZJ ddlKmLZL ddlMmNZNmOZO ddlPmQZQmRZRmSZSmTZT ddlUmVZVmWZWmXZX ddlYmZZZm[Z[m\Z\m]Z] ddl^m_Z_ ddl`maZa ddlbmcZc ddldmeZe ddlfmgZgmhZh ddlimjZjmkZkmlZl ddlmmnZnmoZompZp ddlqmrZrmsZsmtZtmuZumvZv dd lwmrZx dd!lymzZz dd"l{m|Z|m}Z}m~Z~ dd#lmZmZmZ dd$lmZ erdd%l*mZ dd&lmZ  ej                  e      Z ej                   e$j                         '      Z e!e,e      ZdXd(Zd)ZdYd*Z	 	 dZ	 	 	 	 	 	 	 d[d+Z	 d\	 	 	 	 	 d]d,Z	 	 	 	 	 	 d^d-Z	 	 	 	 	 	 d_d.Zd`d/Zdad0Z e d1er2      Zeeef   ee   z  ez  Zd3ed4<   eez  Zd3ed5<    G d6 d7e.      Z G d8 d9e      Zdbd:Zdcd;Z	 	 	 	 ddd<Zded=Zdfd>Zdgd?Zdgd@ZdhdAZddB	 	 	 	 	 didCZ	 	 	 	 	 	 djdDZ G dE dFe      Z	 dk	 	 	 	 	 dldGZ	 dk	 	 	 	 	 dldHZdmdIZdndJZ	 	 	 	 dodKZ	 	 	 	 	 	 dpdLZdqdMZdqdNZ	 	 	 	 drdOZdsdPZ	 	 	 	 dtdQZdudRZdvdSZdwdTZdxdUZ	 	 	 dy	 	 	 	 	 	 	 	 	 dzdVZ	 	 	 	 d{	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d|dWZy)}zOpenAI chat wrapper.    )annotationsN)AsyncIterator	AwaitableCallableIteratorMappingSequence)partial)BytesIO)JSONDecodeError)ceil)
itemgetter)TYPE_CHECKINGAnyLiteral	TypeAliasTypeVarcast)urlparse)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LanguageModelInputModelProfileRegistry)BaseChatModelLangSmithParams)	AIMessageAIMessageChunkBaseMessageBaseMessageChunkChatMessageChatMessageChunkFunctionMessageFunctionMessageChunkHumanMessageHumanMessageChunkInvalidToolCallSystemMessageSystemMessageChunkToolCallToolMessageToolMessageChunkis_data_content_block)content)InputTokenDetailsOutputTokenDetailsUsageMetadata)_convert_from_v03_ai_messageconvert_to_openai_data_block)tool_call_chunk)JsonOutputParserPydanticOutputParser)JsonOutputKeyToolsParserPydanticToolsParsermake_invalid_tool_callparse_tool_call)ChatGenerationChatGenerationChunk
ChatResult)RunnableRunnableLambdaRunnableMapRunnablePassthrough)run_in_executor)BaseTool)
_stringify)get_pydantic_field_names)convert_to_openai_functionconvert_to_openai_tool)PydanticBaseModelTypeBaseModelis_basemodel_subclass)_build_model_kwargsfrom_envsecret_from_env)	BaseModel
ConfigDictField	SecretStrmodel_validator)rM   )Self)_get_default_async_httpx_client_get_default_httpx_client _resolve_sync_and_async_api_keys)$_convert_from_v1_to_chat_completions_convert_from_v1_to_responses_convert_to_v03_ai_message)	_PROFILES)ModelProfile)Response)cafilec                T    t         j                  |       xs i }|j                         S N)_MODEL_PROFILESgetcopy)
model_namedefaults     _/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_openai/chat_models/base.py_get_default_model_profilere      s#    !!*-3G<<>    )file_searchweb_search_preview
web_searchcomputer_use_previewcode_interpretermcpimage_generationc           
         | j                  d      }| j                  d      }| j                  d      }|dk(  rt        | j                  dd      ||      S |dk(  r| j                  dd      xs d}i }| j                  d	      x}rt        |      |d	<   g }g }| j                  d
      x}	r$|	D ]  }
	 |j                  t	        |
d             ! | j                  d      x}r||d<   t        ||||||      S |dv r*|dk(  rd|ini }t        | j                  dd      |||      S |dk(  r;t        | j                  dd      t        t        | j                  d            |      S |dk(  rKi }d| v r| d   |d<   t        | j                  dd      t        t        | j                  d            |||      S t        | j                  dd      ||      S # t
        $ r/}|j                  t        |
t        |                   Y d}~Zd}~ww xY w)zConvert a dictionary to a LangChain message.

    Args:
        _dict: The dictionary.

    Returns:
        The LangChain message.
    rolenameiduserr-    )r-   rq   rp   	assistantfunction_call
tool_callsT)	return_idNaudio)r-   additional_kwargsrp   rq   rv   invalid_tool_callssystem	developerr}   __openai_role__)r-   rp   rq   ry   functionr-   rp   rq   tooltool_call_id)r-   r   ry   rp   rq   r-   ro   rq   )r`   r$   dictappendr9   	Exceptionr8   strr   r'   r"   r   r*   r    )_dictro   rp   id_r-   ry   ru   rv   rz   raw_tool_callsraw_tool_callerx   s                rd   _convert_dict_to_messager      sJ    99VD99VD
))D/Cv~EIIi$<4PP{ ))Ir*0b"$!IIo66=615m1Do.
"YY|44>4!/ %%omt&TU IIg&&5&).g&/!1
 	
 &&9=9L.5RTIIi,/	
 	
 zIIi,4UYYv=N3OTW
 	
 v~U?(-ff%IIi,c599^#<=/
 	
 uyyB7dsKKK ! &--.}c!fE s   2G	H$HHc           	     H   | rt        | t              rg }| D ]  }t        |t              rd|v r|d   dv s|d   dv r|dk(  r,t        |t              rUt        |      rJ|dk(  r(t	        |      j                         j                  d      s|j                  t        ||             t        |t              r|j                  d      dk(  r|j                  d	      x}rt        |t              r|j                  d      d
k(  rD|j                  d      x}r1|j                  d      x}r|j                  ddd| d| id       0|j                  d      dk(  r+|j                  d      x}r|j                  dd|id       oq|j                  |        |S | }|S )zFormat message content.type)tool_usethinkingreasoning_content)ru   code_interpreter_callchat/completions	responsesaiapiimagesourcebase64
media_typedata	image_urlurlzdata:z;base64,r   r   )

isinstancelistr   r,   r   lower
startswithr   r2   r`   )	r-   r   ro   formatted_contentblockr   r   r   r   s	            rd   _format_message_contentr      s    :gt, .	0E 5$'eO&M%RRf)SS#55 5$')%0 +D	0A0L0LT0R!(()EeQT)UV 5$'IIf%0$yy22V2vt,::f%1#)::l#;;Z;!'F!333%,,$/*/5HTF1S)T ZZ'50VZZ=N6Nc6N%,,!,E3<H !((/].	0d  $rf   c                   dt        | j                  || j                        i}| j                  xs | j                  j                  d      x}||d<   t        | t              r| j                  |d<   |S t        | t              rd|d<   |S t        | t              rd|d<   | j                  s| j                  rK| j                  D cg c]  }t        |       c}| j                  D cg c]  }t        |       c}z   |d<   nd| j                  v rX| j                  d   |d<   h d	}|d   D cg c]+  }|j                         D ci c]  \  }}||v s|| c}}- c}}}|d<   n"d
| j                  v r| j                  d
   |d
<   n	 d
|v sd|v r|d   xs d|d<   d}	| j                  D ]F  }
t        |
t               s|
j                  d      dk(  s)|
j                  d      x}s=|dk7  sCd|i}	H |	s7d| j                  v r)| j                  d   }d|v rd| j                  d   d   in|}	|	r|	|d<   |S t        | t"              r!| j                  j                  dd      |d<   |S t        | t$              rd|d<   |S t        | t&              rBd|d<   | j(                  |d<   h d}|j                         D ci c]  \  }}||v s|| }}}|S d|  }t+        |      c c}w c c}w c c}}w c c}}}w c c}}w )zDConvert a LangChain message to dictionary format expected by OpenAI.r-   )r   ro   rp   Nro   rr   rt   rv   >   rq   r   r   ru   r   rx   rq   r   r~   r|   r   r   r   >   ro   r-   r   zGot unknown type )r   r-   r   rp   ry   r`   r   r    ro   r$   r   rv   rz   !_lc_tool_call_to_openai_tool_call)_lc_invalid_tool_call_to_openai_tool_callitemsr   r'   r"   r*   r   	TypeError)messager   message_dictrp   tctool_call_supported_props	tool_callkvrx   r   r   	raw_audiosupported_propsmsgs                  rd   _convert_message_to_dictr      sr    	*7??',,W$L E 9 9 = =f EER#V ';'&||V@  
G\	*%V| { 
GY	'*V!;!;@G@R@R*:<1"5* "44 :"=*L& W666)0)B)B<)PL&(B% ".l!;* * #,//"3V$!Qq<U7UAV*L&  9 99 -4,E,Eo,VL)l*ll.J&29&=&EL#'+__ 		$E5$'IIf%0!IIdO+S+;& s		$ G$=$==11':I 9$ w009$?@ 
 $)L!   
G]	+&88<<x 
V  
G_	-)V  
G[	)%V'.';';^$=)5););)=VAoAU1VV  "'+ns* W*Z Ws6   	K%+K*3K5K/K/K5>K<K</K5c           
        | j                  d      }t        t        | j                  d            }t        t        | j                  d      xs d      }i }| j                  d      r!t        | d         }d|v r
|d   d|d<   ||d<   g }| j                  d      x}rS	 |D 	cg c]G  }	t	        |	d   j                  d      |	d   j                  d	      |	j                  d      |	d
         I }}	|dk(  s	|t        k(  rt        ||      S |dk(  s	|t        k(  rt        ||||      S |dv s	|t        k(  r|dk(  rddi}ni }t        |||      S |dk(  s	|t        k(  rt        || d   |      S |dk(  s	|t        k(  rt        || d   |      S |s	|t        k(  rt        |||      S  |||      S c c}	w # t
        $ r Y w xY w)z%Convert to a LangChain message chunk.rq   ro   r-   rs   ru   rp   rv   r   	argumentsindex)rp   argsrq   r   rr   )r-   rq   rt   )r-   ry   rq   tool_call_chunksr{   r}   r~   )r-   rq   ry   r   r   r   )r-   r   rq   r   )r`   r   r   r   r3   KeyErrorr%   r   r(   r#   r+   r!   )
r   default_classr   ro   r-   ry   ru   r   r   rtcs
             rd   _convert_delta_to_message_chunkr   p  s    ))D/CUYYv&'D3		),23G yy!U?34]"}V'<'D$&M&!-:/*<00~0	 *    Z,,V4Z,,[9wwt}g,	    v~*;; S99{m~=/-	
 	
 &&-;M*M;!2K @ "!7H
 	
 z].BB#G%-CPPv~*::%*?C
 	
 } 00dsCCS11K   		s%   F5 "AF0.F5 0F5 5	G Gc                   t        |t              r:t        | t              s%dt        |       dt        |        }t        |      || z   S t        |t              rst        | t              s%dt        |       dt        |        }t        |      |j                         D ci c]"  \  }}|t        | j                  |d      |      $ c}}S t        j                  dt        |              |S c c}}w )Nz%Got different types for token usage: z and r   z!Unexpected type for token usage: )
r   intr   
ValueErrorr   r   _update_token_usager`   warningswarn)overall_token_usage	new_usager   r   r   s        rd   r   r     s   
 )S!-s37	?#5.A)B(CE  S/!...)T"-t47	?#5.A)B(CE  S/! ")
1 "#6#:#:1a#@!DD
 	
 MM5d9o5FGH
s   #'C1c                    d| j                   v rd}t        j                  |       | d| j                   v rd}t        j                  |       |  )NzH'response_format' of type 'json_schema' is not supported with this modelzThis model does not support OpenAI's structured output feature, which is the default method for `with_structured_output` as of langchain-openai==0.3. To use `with_structured_output` with this model, specify `method="function_calling"`.z"Invalid schema for response_formata3  Invalid schema for OpenAI's structured output feature, which is the default method for `with_structured_output` as of langchain-openai==0.3. Specify `method="function_calling"` instead or update your schema. See supported schemas: https://platform.openai.com/docs/guides/structured-outputs#supported-schemas)r   r   r   )r   r   s     rd   _handle_openai_bad_requestr     s[    R	
3 	 	g+qyy8[ 	 	g	rf   c                    | syd| v S )NFzgpt-5.2-pro )rb   s    rd   _model_prefers_responses_apir     s    J&&rf   _BM)boundr   _DictOrPydanticClass_DictOrPydanticc                      e Zd ZU dZ edd      Zded<    edd      Zded<    edd      Zded<    edd      Z	ded	<    ed
d      Z
ded<   	 dZded<   	  ee      Zded<   	  ed edd            Zded<   	  edd      Zded<   	  edd      Zded<   	  e edd            Zded <    edd!      Zd"ed#<   	 dZd$ed%<   	 dZd&ed'<   	 dZded(<   	 dZded)<   	 dZd&ed*<   	 dZd$ed+<   	 dZd&ed,<   	 dZd-ed.<   	 d/Zd0ed1<   	 dZd&ed2<   	 dZded3<   	  ed      Z d&ed4<   	 dZ!ded5<   	 dZ"d6ed7<   	 dZ#ded8<   	 dZ$ded9<   	 dZ%d:ed;<   dZ&d<ed=<    edd      Z'd>ed?<   	  edd      Z(d>ed@<   	  eddA      Z)dBedC<   	 dZ*dDedE<   	 d/Z+d0edF<   	  ed      Z,d6edG<   	 dZ-dHedI<   	 dZ.dedJ<   	 dZ/d$edK<   	 dZ0dedL<   	 d/Z1d0edM<   	 dZ2d$edN<   	  e edOd            Z3dedP<   	  e4dQ      Z5 e6dRS      e7dwdT              Z8 e6dRS      e7dwdU              Z9 e6dVS      dxdW       Z: e6dVS      dxdX       Z;e<dydY       Z=dzdZZ>	 	 	 	 	 	 	 	 d{d[Z?d|d\Z@	 	 d}	 	 	 	 	 	 	 	 	 d~d]ZA	 	 d}	 	 	 	 	 	 	 	 	 dd^ZB	 d	 	 	 	 	 dd_ZC	 	 d}dd`	 	 	 	 	 	 	 	 	 	 	 ddaZD	 	 d}	 	 	 	 	 	 	 	 	 ddbZEddcZFddd	 	 	 	 	 	 	 ddeZG	 d	 	 	 	 	 ddfZH	 	 d}dd`	 	 	 	 	 	 	 	 	 	 	 ddgZI	 	 d}	 	 	 	 	 	 	 	 	 ddhZJe<dydi       ZK	 d	 	 	 	 	 d fdjZL	 d	 	 	 	 	 ddkZMe<ddl       ZNddmZOd fdnZP	 d	 	 	 	 	 d fdoZQdddddp	 	 	 	 	 	 	 	 	 	 	 	 	 d fdqZR	 ddrd/ddds	 	 	 	 	 	 	 	 	 	 	 	 	 ddtZSdduZT	 	 	 	 ddvZU xZVS )BaseChatOpenAIz:Base wrapper around OpenAI large language models for chat.NT)rc   excluder   clientasync_clientroot_clientroot_async_clientgpt-3.5-turbomodelrc   aliasr   rb   zfloat | Nonetemperature)default_factorydict[str, Any]model_kwargsapi_keyOPENAI_API_KEY)rc   )r   r   zCSecretStr | None | Callable[[], str] | Callable[[], Awaitable[str]]openai_api_keybase_url
str | Noneopenai_api_baseorganizationopenai_organizationOPENAI_PROXYopenai_proxytimeoutz(float | tuple[float, float] | Any | Nonerequest_timeoutbool | Nonestream_usage
int | Nonemax_retriespresence_penaltyfrequency_penaltyseedlogprobstop_logprobszdict[int, int] | None
logit_biasFbool	streamingntop_p
max_tokensreasoning_effortdict[str, Any] | None	reasoning	verbositytiktoken_model_namezMapping[str, str] | Nonedefault_headerszMapping[str, object] | Nonedefault_queryz
Any | Nonehttp_clienthttp_async_clientstop_sequenceszlist[str] | str | NonestopzMapping[str, Any] | None
extra_bodyinclude_response_headersdisabled_paramslist[str] | Noneincludeservice_tierstore
truncationuse_previous_response_iduse_responses_apiLC_OUTPUT_VERSIONoutput_version)populate_by_namebefore)modec                0    t        |       }t        ||      S )z>Build extra kwargs from additional params that were passed in.)rD   rJ   )clsvaluesall_required_field_namess      rd   build_extrazBaseChatOpenAI.build_extraG  s     $<C#@ "6+CDDrf   c                   |j                  d      xs |j                  d      xs d}|j                         }|j                  d      r	d|vrd|d<   |j                  d      rid|vre|j                  d	      d
k7  rQ|j                  d      xs i j                  d      d
k7  r*|j                  d      }||dk7  r|j                  dd       |S )zValidate temperature parameter for different models.

        - gpt-5 models (excluding gpt-5-chat) only allow `temperature=1` or unset
            (Defaults to 1)
        rb   r   rs   o1r      gpt-5chatr   noner   effortN)r`   r   r   pop)r  r  r   model_lowerr   s        rd   validate_temperaturez#BaseChatOpenAI.validate_temperatureN  s     

<(EFJJw,?E2kkm !!$'M,G$%F=! ""7+{*

-.&8K(.B33H=G **]3K&;!+; 

=$/rf   afterc                @     j                    j                   dk  rd}t        |       j                   ( j                   dkD  r j                  rd}t        |       j                  xs, t	        j
                  d      xs t	        j
                  d       _         j                  xs t	        j
                  d       _        t         fdd	D              rd
t        j                  vrd _	        d}d} j                  t         j                        \  }} j                   j                   j                   j                   j                  d} j                   j                  |d<    j                   rR j"                  s j$                  r: j                   } j"                  } j$                  }d|d|d|}t        |       j&                  s|d _        d _        n j                   r7 j"                  s+	 ddl}|j/                   j                   t0               _         j"                  xs  t3         j                   j                        |d}
t5        j6                  di ||
 _         j(                  j8                  j:                   _         j<                  s j                   r7 j$                  s+	 ddl}|j?                   j                   t0               _         j$                  xs  tA         j                   j                        |d}t5        jB                  di || _"         jD                  j8                  j:                   _         S # t,        $ r}	d}t-        |      |	d}	~	ww xY w# t,        $ r}	d}t-        |      |	d}	~	ww xY w)z?Validate that api key and python package exists in environment.Nr  zn must be at least 1.zn must be 1 when streaming.OPENAI_ORG_IDOPENAI_ORGANIZATIONOPENAI_API_BASEc              3  <   K   | ]  }t        |d       d u   y wr^   )getattr).0keyselfs     rd   	<genexpr>z6BaseChatOpenAI.validate_environment.<locals>.<genexpr>  s'       c4(D0s   )
r   r   r   r   r   r   r   r   r  r  OPENAI_BASE_URLT)r   r   r   r  r  r   zwCannot specify 'openai_proxy' if one of 'http_client'/'http_async_client' is already specified. Received:
openai_proxy=z
http_client=z
http_async_client=r   zRCould not import httpx python package. Please install it with `pip install httpx`.)proxyverify)r  r   r   )#r   r   r   r   osgetenvr   allenvironr   r   rU   r   r  r  r   r   r  r  r   r   httpxImportErrorClientglobal_ssl_contextrT   openaiOpenAIr  completionsr   AsyncClientrS   AsyncOpenAIr   )r.  r   sync_api_key_valueasync_api_key_valueclient_paramsr   r  r  r7  r   sync_specificasync_specifics   `           rd   validate_environmentz#BaseChatOpenAI.validate_environmentn  s    66$&&1*)CS/!66$&&1*/CS/! $$ 0yy)0yy./ 	 
  $33SryyAR7S    "3 $D >BIM* 7W##73 3
 !44,,++#33!//
 '+/+;+;M-($"2"2d6L6L,,L**K $ 6 6!/K>1F4E3GI 
 S/!{{!) ##' $$T-=-=6$ (-||"//8J (4 (D$ $(#3#3 $0,,d.B.B  2! $*==#R=#RM#R "..33??    )?)?2  */):):++4F *; *&  $55  2(($*>*> /N &,%7%7 && &D" !% 6 6 ; ; G GDW ' 6J  *#.A56, # 2F  &c*12s0   M" N "	M>+M99M>	N
NNc                R    | j                   t        | j                        | _         | S )z$Set model profile if not overridden.)profilere   rb   r.  s    rd   _set_model_profilez!BaseChatOpenAI._set_model_profile  s#     <<5dooFDLrf   c                   i d| j                   d| j                  d| j                  d| j                  d| j                  d| j
                  d| j                  d| j                  xs d	d
| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                  d| j                   | j"                  | j$                  d}| j&                  | j(                  d|j+                         D ci c]  \  }}|	|| c}}| j,                  S c c}}w )2Get the default parameters for calling OpenAI API.r   r   r   r   r   r   r   r  Nr   r  r   r   r   r   r   r  r  )r  r  )r   stream)r   r   r   r   r   r   r   r  r   r  r   r   r   r   r   r  r  r  r  rb   r   r   r   )r.  exclude_if_noner   r   s       rd   _default_paramszBaseChatOpenAI._default_params  s   
 5 5
!7!7
 DII
 TZZ	

 
 D--
 $//
 DII%
 $//
 $//
 
 4++
  5 5
 
 
  t||!
" D--#
$ //ZZ'
. __nn
 !0 5 5 7I11=q!tI
 	
 	
 Js   (
E	3E	c                
   i }d }|D ]c  }||j                  d      }|7|j                         D ]$  \  }}|	||v rt        ||   |      ||<    |||<   & |S|j                  d      }e || j                  d}|r||d<   |S )Ntoken_usagesystem_fingerprint)rP  rb   )r`   r   r   rb   )	r.  llm_outputsr   rQ  outputrP  r   r   combineds	            rd   _combine_llm_outputsz#BaseChatOpenAI._combine_llm_outputs  s    $&!! 	FF~ **]3K&'--/ 3DAqy //1D/2A2+A. 23+A.3 ")%+ZZ0D%E"!	F" $7dooV-?H)*rf   c                Z   |j                  d      dk(  ry |j                  d      }|j                  dg       xs" |j                  di       j                  dg       }|rt        ||j                  d            nd }t        |      dk(  rPt         |d|	      |
      }| j                  dk(  r*g |j
                  _        d|j
                  j                  d<   |S |d   }|d   y t        |d   |      }	|ri |ni }
|j                  d      x}rM||
d<   |j                  d      x}r||
d<   |j                  d      x}r||
d<   |j                  d      x}r||
d<   |j                  d      }|r||
d<   |rt        |	t              r||	_        d|	j                  d<   t        |	|
xs d 
      S )Nr   zcontent.deltausagechoiceschunkr  r   rs   )r-   usage_metadatar   generation_infov1r  deltafinish_reasonr   rb   rQ  r   r;  model_provider)r`   _create_usage_metadatalenr;   r  r   r-   response_metadatar   r   r   rZ  )r.  rY  default_chunk_classbase_generation_inforP  rX  rZ  generation_chunkchoicemessage_chunkr\  r_  rb   rQ  r  r   s                   rd   "_convert_chunk_to_generation_chunkz1BaseChatOpenAI._convert_chunk_to_generation_chunk+  s    99V/ii(IIi$ 9yy"%)))R8 	  #;		.0IJ 	
 w<12+B~V 4  ""d*35 ((0OS ((::;KL##'?"77O0
 7K212PR"JJ77=7/<OO,"YYw//z/0:-%*YY/C%DD!D8J 45$yy88|82>/::j)*2OJ'jG+9M(<D''(89"!?3Jd
 	
rf   c                6    | j                   d}t        |      y)z8Check that sync client is available, raise error if not.NzSync client is not available. This happens when an async callable was provided for the API key. Use async methods (ainvoke, astream) instead, or provide a string or sync callable for the API key.)r   r   )r.  r   s     rd   _ensure_sync_client_availablez,BaseChatOpenAI._ensure_sync_client_availablei  s(    ;;Q 
 S/! rf   c              +    K   | j                          d|d<    | j                  |fd|i|}| j                  rX | j                  j                  j
                  j                  di |}|j                         }dt        |j                        i}n( | j                  j
                  j                  di |}i }|j                  d      }	|5 }
d}d}d}d}d}|
D ]l  }|r|ni }t        |||||	||| j                        \  }}}}|s.|r|j                  |j                  |	       d}d
|j                  j                   v rd}| n 	 d d d        y # 1 sw Y   y xY wwNTrL  r  headersresponse_formatF)schemametadatahas_reasoningr  rY  r   r   )rk  _get_request_payloadr  r   with_raw_responser   createparser   rn  r`   ,_convert_responses_chunk_to_generation_chunkr  on_llm_new_tokentextr   ry   r.  messagesr  run_managerkwargspayloadraw_context_managercontext_managerrn  original_schema_objresponseis_first_chunkcurrent_indexcurrent_output_indexcurrent_sub_indexrs  rY  rr  rf  s                      rd   _stream_responsesz BaseChatOpenAI._stream_responsess  s     	**,x+$++HJ4J6J(("U$"2"2"D"D"N"N"U"U ## 2779O $':'B'B"CDG?d..88??J'JOG$jj):; 	+!NM#%  "!M! +&47" A!(%.%"/#'#6#6	!(%$ $"#44,119I 5  &+N"&6&>&>&P&PP(,**3+	+ 	+ 	+s%   C
E:EAE		EEEc               &  K   d|d<    | j                   |fd|i|}| j                  r` | j                  j                  j                  j
                  di | d {   }|j                         }dt        |j                        i}n0 | j                  j                  j
                  di | d {   }i }|j                  d      }	|4 d {   }
d}d}d}d}d}|
2 3 d {   }|r|ni }t        |||||	||| j                        \  }}}}|s4|r%|j                  |j                  |	       d {    d}d
|j                  j                  v rd}| }7 7 7 7 7 /6 d d d       d {  7   y # 1 d {  7  sw Y   y xY wwrm  )ru  r  r   rv  r   rw  rx  r   rn  r`   ry  r  rz  r{  r   ry   r|  s                      rd   _astream_responsesz!BaseChatOpenAI._astream_responses  s      x+$++HJ4J6J((Od,,>>HHOO    
 2779O $':'B'B"CDG$KD$:$:$D$D$K$K$Vg$VVOG$jj):;" 	+ 	+h!NM#%  "!M' + +e&47" A!(%.%"/#'#6#6	!(%$ $")::,119I ;    &+N"&6&>&>&P&PP(,**U W	++&'  (	+ 	+ 	+ 	+ 	+s   AFEAF2E!3FE#FE<!E)%E%&E))+E<"E<7E'8&E<F!F#F%E)'E<)E<*F5E86F<FFF
Fc                   ||j                  di       j                  d      | j                  j                  di       j                  d      | j                  g}|D ]  }t        |t              s|c S  | j                  xs dS )zDetermine whether to include usage metadata in streaming output.

        For backwards compatibility, we check for `stream_options` passed
        explicitly to kwargs or in the `model_kwargs` and override `self.stream_usage`.
        stream_optionsinclude_usageF)r`   r   r   r   r   )r.  r   r  stream_usage_sourcesr   s        rd   _should_stream_usagez#BaseChatOpenAI._should_stream_usage  s     JJ',00A!!"2B7;;OL	 
 + 	F&$'	   )E)rf   )r   c             +    K   | j                          d|d<    | j                  |fi |}|rd|i|d<    | j                  |fd|i|}t        }i }d|v ro| j                  rt        j                  d       |j                  d        | j                  j                  j                  j                  j                  di |}	|	}
nx| j                  rN | j                  j                  j                  di |}|j!                         }dt#        |j$                        i}n | j                  j                  di |}|}
	 |
5 }d}|D ]  }t'        |t"              s|j)                         }| j+                  |||r|ni       }|=|j,                  j.                  }|j0                  xs i j3                  d	      }|r|j5                  |j6                  ||
       d}|  	 d d d        t?        d      rJd|v rE|jA                         }| jC                  |      }|r|j5                  |j6                  |       | y y y # 1 sw Y   `xY w# t8        j:                  $ r}t=        |       Y d }~d }~ww xY wwNTrL  r  r  r  ro  zLCannot currently include response headers when response_format is specified.rn  r   )rY  r   Fget_final_completionrt  r   )"rk  r  ru  r   r  r   r   r"  r   betar  r=  rL  r   rv  rw  rx  r   rn  r   
model_dumpri  r   	__class__r\  r`   rz  r{  r;  BadRequestErrorr   hasattrr  %_get_generation_chunk_from_completionr.  r}  r  r~  r   r  r  rd  re  response_streamr  raw_responser  r  rY  rf  r   r   final_completions                      rd   _streamzBaseChatOpenAI._stream  s     	**,x0t00HH(7'FF#$+$++HJ4J6J6D!',,! KK!Kd..3388DDKKVgVO-O,,Ct{{<<CCNgN'--/(148L8L3M'N$-4;;--88&O	*  +H!%% +E%eT2 % 0 0 2'+'N'N+0>,B($
 (/ *:*B*B*L*L' 0 @ @ FBKKJWH"#44,11"2%- 5 
 &+N**'++0 8349Jg9U'<<>#II   ,,$))1A -  #" :V41+ +, %% 	*&q))	*sJ   D:I5=I
 ?BH>I
 'AI5>II
 
I2I-(I5-I22I5c                ~   | j                           | j                  |fd|i|}d }d }	 d|v r^|j                  d       	  | j                  j                  j
                  j                  j                  di |}|j                         }n| j                  |      r|j                  d      }
|
r<t        |
      r1 | j                  j                  j                  j                  di |}n0 | j                  j                  j                  j                  di |}|j                         }| j                   rdt#        |j$                        i}t'        ||
|| j(                        S  | j*                  j                  j                  di |}|j                         }| j                   r%|#t/        |d      rdt#        |j$                        i}| j5                  |      S # t        j                  $ r}	t        |	       Y d }	~	fd }	~	ww xY w# t,        $ r&}	|t/        |d      r|j0                  |	_        |	d }	~	ww xY wNr  ro  rL  rn  )rq  rr  r  http_responser   )rk  ru  r"  r   r  r=  rv  rx  r;  r  r   _use_responses_apir`   _is_pydantic_classr   rw  r  r   rn  '_construct_lc_result_from_responses_apir  r   r   r  r  r  _create_chat_resultr.  r}  r  r~  r  r  r\  r  r  r   r  s              rd   	_generatezBaseChatOpenAI._generate:  s6    	**,+$++HJ4J6J%	 G+H%2Q((--99KKQQ % !
  ,113H ((1&,jj1B&C#&+=>Q+R#U4#3#3#=#=#O#O#U#U $!$L $W4#3#3#=#=#O#O#V#V $!$L (--/00'0$|7K7K2L&MO>.,#'#6#6	   Dt{{<<CCNgN'--/ ))(i0($|/C/C*DEO''/BBE -- 2.q1120  	'GL/,R)77
G	sI   H A
G" CH )6H "H
5H H H

H 	H<!H77H<c                   t        | j                  t              r| j                  S | j                  dk(  sE| j                  9| j
                  -| j                  !| j                  st        | j                        ryt        |      S )Nresponses/v1T)r   r  r   r  r  r   r  r  r   rb   r  )r.  r  s     rd   r  z!BaseChatOpenAI._use_responses_apis  sm    d,,d3)))>1||'~~)*,,+DOO<!'**rf   r  c                  | j                  |      j                         }|||d<   i | j                  |}| j                  |      rC| j                  r)t        |      \  }}|r|n|}|r||d<   t        ||      }|S t        ||      }|S |D 	cg c]1  }	t        |	t              rt        t        |	            n
t        |	      3 c}	|d<   |S c c}	w )Nr  previous_response_idr}  )_convert_inputto_messagesrN  r  r  _get_last_messages _construct_responses_api_payloadr   r   r   rV   )
r.  input_r  r  r}  r  last_messagesr  payload_to_usems
             rd   ru  z#BaseChatOpenAI._get_request_payload  s     &&v.::<!F6N4T))4V4""7+,,6H6R332FH'6JG23:>7S  ;8WM  "	#  a+ ))Ma)PQ-a01#GJ #s   6Cc                   g }t        |t              r|n|j                         }|j                  d      rt	        |j                  d            	 |d   }|d}t        |      |j                  d      }|j                  d      }	|D ]  }
t        |
d         }|r!t        |t              rt        ||	      |_        |xs i }|
j                  d      |
j                  d      n|j                  d      |d<   d	|
v r|
d	   |d	<   t        ||
      }|j                  |        |d|j                  d| j                        |j                  dd      d}d|v r|d   |d<   |	r|	|d<   t        |t        j                         rt#        |dd       r}|j$                  d   j&                  }t)        |d      r&|j*                  |d   j&                  j,                  d<   t)        |d      r&|j.                  |d   j&                  j,                  d<   t1        ||      S # t
        $ r$}d|j                          }t        |      |d }~ww xY w)NerrorrX  z Response missing `choices` key: z0Received response with null value for `choices`.rW  r  r   r_  r   r[  r;  r   rQ  rs   )rP  r`  rb   rQ  rq   r   parsedrefusal)generations
llm_output)r   r   r  r`   r   r   keysr   r   r   ra  rZ  r:   r   rb   r;  rM   r+  rX  r   r  r  ry   r  r<   )r.  r  r\  r  response_dictrX  r   r   rP  r  resr   genr  s                 rd   r  z"BaseChatOpenAI._create_chat_result  sd   
  #8T2H8K8K8M 	 W%]..w788	'#I.G
 ?DCC. #''0$((8 	$C.s9~>Gz'9=)?*& .3O 77?+7 ($((9 O,
 S .1*o
+ /RCs#	$" '&'++GT__E"/"3"34H""M	

 = ,T2Jt)5J~&h 0 01gi7
 &&q)11Gw)EL^^A&&88Bw	*FMooA&&88CkjII_  	'4]5G5G5I4JKC3-Q&	's   H 	I#IIc              ~  K   d|d<    | j                   |fi |}|rd|i|d<    | j                  |fd|i|}t        }i }d|v ro| j                  rt	        j
                  d       |j                  d        | j                  j                  j                  j                  j                  di |}	|	}
n| j                  rV | j                  j                  j                  di | d {   }|j                         }dt!        |j"                        i}n$ | j                  j                  di | d {   }|}
	 |
4 d {   }d}|2 3 d {   }t%        |t               s|j'                         }| j)                  |||r|ni       }|C|j*                  j,                  }|j.                  xs i j1                  d	      }|r&|j3                  |j4                  ||
       d {    d}| 7 7 7 7 7 6 d d d       d {  7   n# 1 d {  7  sw Y   nxY wn+# t6        j8                  $ r}t;        |       Y d }~nd }~ww xY wt=        d      r]d|v rX|j?                          d {  7  }| jA                  |      }|r&|j3                  |j4                  |       d {  7   | y y y wr  )!r  ru  r   r  r   r   r"  r   r  r  r=  rL  r   rv  rw  rx  r   rn  r   r  ri  r   r  r\  r`   rz  r{  r;  r  r   r  r  r  r  s                      rd   _astreamzBaseChatOpenAI._astream  s      x0t00HH(7'FF#$+$++HJ4J6J6D!',,! KK!Qd4499>>JJQQ O .O,,%OT%6%6%H%H%O%O &&   (--/(148L8L3M'N$!9!2!2!9!9!DG!DD&O	*& + +(!%#+ + +%%eT2 % 0 0 2'+'N'N+0>,B($
 (/ *:*B*B*L*L' 0 @ @ FBKKJWH")::,11"2%- ;   
 &+N**=  E++ $,+ + + + +, %% 	*&q))	*8349Jg9U%-%B%B%DDD#II   !22$))1A 3    #" :V4s   C'J=)G4*AJ=5G76J==H( G9H( HG?G;G?BH'G=(H4J=7J=9H( ;G?=H?H H( HH( H$HH$ H( 'J=(I;IJ=I&J=6I978J=/J20J=c                  K    | j                   |fd|i|}d }d }	 d|v rf|j                  d       	  | j                  j                  j                  j
                  j                  di | d {   }|j                         }n*| j                  |      r|j                  d      }
|
rDt        |
      r9 | j                  j                  j
                  j                  di | d {   }n8 | j                  j                  j
                  j                  di | d {   }|j                         }| j                  rdt!        |j"                        i}t%        ||
|| j&                        S  | j(                  j
                  j                  di | d {   }|j                         }| j                  r%|#t-        |d      rdt!        |j"                        i}t3        d | j4                  |       d {   S 7 # t        j                  $ r}	t        |	       Y d }	~	wd }	~	ww xY w7 G7 7 # t*        $ r&}	|t-        |d      r|j.                  |	_        |	d }	~	ww xY w7 lwr  )ru  r"  r   r  r=  rv  rx  r;  r  r   r  r`   r  r   rw  r  r   rn  r  r  r   r   r  r  r  rA   r  r  s              rd   
_ageneratezBaseChatOpenAI._agenerate/  sx     ,$++HJ4J6J)	 G+H%2)h)?)?)D)D)P)P)b)b)h)h *!* $L  ,113H ((1&,jj1B&C#&+=>Q+RVd44>>PPVV %  ! Xd44>>PPWW %  !
 (--/00'0$|7K7K2L&MO>.,#'#6#6	  &PT%6%6%H%H%O%O &&   (--/ ))(i0($|/C/C*DEO$$**Ho
 
 	
Y$ -- 2.q112   	'GL/,R)77
G	
s   I,H8 =H 0H1H A$H8 )H0*8H8 "H3#AH8 2I,3)H8 H6H8 1AI,=I*>I,H H-H(#H8 (H--H8 3H8 6H8 8	I'!I""I''I,c                6    d| j                   i| j                  S )zGet the identifying parameters.rb   )rb   rN  rH  s    rd   _identifying_paramsz"BaseChatOpenAI._identifying_paramsm  s     dooF1E1EFFrf   c                :   d| j                   it        | 	  |      | j                  |}|j	                  d      x}rUt        |t              rE|D cg c]6  }t        |t              r"|j	                  d      dk(  rd|v ri |ddin|n|8 c}|d<   |S c c}w )z,Get the parameters used to invoke the model.r   r  toolsr   rl   rn  z**REDACTED**)rb   super_get_invocation_paramsrN  r`   r   r   r   )r.  r  r  paramsr  r   r  s         rd   r  z%BaseChatOpenAI._get_invocation_paramsr  s    
 T__
g,$,7
 ""
 	
 ZZ((E(j.E
 "	  dD)dhhv.>%.G 9BT8I4D4)^4tF7O s   ;Bc           	     p    | j                   dd|i|}t        d|j                  d| j                        d|j                  d| j                              }|j                  d| j
                        xs |j                  d| j
                        x}r||d	<   |xs |j                  dd
      x}r||d<   |S )z Get standard params for tracing.r  r;  r   r  r   )ls_providerls_model_namels_model_typels_temperaturer   max_completion_tokensls_max_tokensNls_stopr   )r  r   r`   rb   r   r   )r.  r  r  r  	ls_paramsr  r  s          rd   _get_ls_paramszBaseChatOpenAI._get_ls_params  s     -,,A$A&A#  **Wdoo> !::mT5E5EF	
	 #JJ|T__E 
#T__J
 
= 
 *7Io&6fjj6676#*Ii rf   c                     y)zvReturn type of chat model.

        Will always return `'openai-chat'` regardless of the specific model name.
        zopenai-chatr   rH  s    rd   	_llm_typezBaseChatOpenAI._llm_type  s     rf   c                   | j                   | j                   }n| j                  }	 t        j                  |      }||fS # t        $ r@ |j                         }d}|j                  d      rd}t        j                  |      }Y ||fS w xY w)Ncl100k_base)zgpt-4ozgpt-4.1r  
o200k_base)r   rb   tiktokenencoding_for_modelr   r   r   get_encoding)r.  r   encodingr#  encoders        rd   _get_encoding_modelz"BaseChatOpenAI._get_encoding_model  s    ##/,,EOOE	62259H h  	6++-K#G%%&DE&,,W5Hh	6s   A   AB	B	c                    | j                   | j                  |      S t        j                  d   dk  rt        |   |      S | j                         \  }}|j                  |      S )z9Get the tokens present in the text with tiktoken package.r     )custom_get_token_idssysversion_infor  get_token_idsr  encode)r.  r{  _encoding_modelr  s       rd   r  zBaseChatOpenAI.get_token_ids  sg    $$0,,T22A!#7(.. 446>$$T**rf   c           
        |t        j                  d       t        j                  d   dk  rt        |   |      S | j                         \  }}|j                  d      rd}d}n'|j                  d      rd}d}nd	| d
}t        |      d}|D 	cg c]  }	t        |	       }
}	|
D ]  }||z  }|j                         D ]p  \  }}|dk(  r|dz  }t        |t              r|D ]  }t        |t              s|d   dk(  r5t        |t              r|d   n|}|t        |j!                  |            z  }Q|d   dk(  r=|d   j#                  d      dk(  r|dz  }vt%        |d   d         }|s|t'        | z  }|d   dk(  rG|t        |j!                  |d   d               z  }|t        |j!                  |d   d               z  }|d   dk(  rt        j                  d       d| }t)        |       n*|s?|t        |j!                  t        |                  z  }|dk(  sl||z  }s  |dz  }|S c c}	w )a  Calculate num tokens for `gpt-3.5-turbo` and `gpt-4` with `tiktoken` package.

        !!! warning
            You must have the `pillow` installed if you want to count image tokens if
            you are specifying the image as a base64 string, and you must have both
            `pillow` and `httpx` installed if you are specifying the image as a URL. If
            these aren't installed image inputs will be ignored in token counting.

        [OpenAI reference](https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb).

        Args:
            messages: The message inputs to tokenize.
            tools: If provided, sequence of `dict`, `BaseModel`, function, or `BaseTool`
                to be converted to tool schemas.
        zECounting tokens in tool schemas is not yet supported. Ignoring tools.r  r  zgpt-3.5-turbo-0301   rp  )r   gpt-4r     zFget_num_tokens_from_messages() is not presently implemented for model z. See https://platform.openai.com/docs/guides/text-generation/managing-tokens for information on how messages are converted to tokens.r   r   r   r{  r   detaillowU   r   r   r   rp   filezEToken counts for file inputs are not supported. Ignoring file inputs.z!Unrecognized content block type

)r   r   r  r  r  get_num_tokens_from_messagesr  r   NotImplementedErrorr   r   r   r   r   r   rb  r  r`   _url_to_size_count_image_tokensr   )r.  r}  r  r   r  tokens_per_messagetokens_per_namer   
num_tokensr  messages_dictr   r-  valuevalr{  
image_sizer  s                    rd   r  z+BaseChatOpenAI.get_num_tokens_from_messages  s   * MMW A!#77AA224x01!" OAB!"O"G $LL  &c**
>FG1!4GG$ ,	2G,,J%mmo *2
U .(!OJeT*$ 2%c3/3v;&3H2<S$2G3v;SD&#hood.C*DDJ [K7";/33H=F *b 0
-9#k:J5:Q-R
'1$, *.A:.N N
 ![J6&# (J0L M+ J '#hooc*of>U.V*WWJ [F2$MM!8
 %Hu"MC",S/1526  #hooc%j&A"BBJ&=/1JU*2,	2\ 	a
a Hs   I)tool_choicestrictparallel_tool_callsro  c                  |||d<   |D cg c]  }t        ||       }}g }	|D ]7  }d|v r|	j                  |d   d          d|v r|	j                  |d          89 |rkt        |t              r"||	v rdd|id}nJ|t        v rd|i}n=|dk(  rd	}n5n4t        |t
              rd	}n!t        |t              rnd
| }
t        |
      ||d<   |rst        |t              rS|j                  d      dk(  r?d|j                  di       v r+|d   j                  dd      }t        t        |d   d         }t        ||      |d<   t        | 0  dd|i|S c c}w )a  Bind tool-like objects to this chat model.

        Assumes model is compatible with OpenAI tool-calling API.

        Args:
            tools: A list of tool definitions to bind to this chat model.

                Supports any tool definition handled by [`convert_to_openai_tool`][langchain_core.utils.function_calling.convert_to_openai_tool].
            tool_choice: Which tool to require the model to call. Options are:

                - `str` of the form `'<<tool_name>>'`: calls `<<tool_name>>` tool.
                - `'auto'`: automatically selects a tool (including no tool).
                - `'none'`: does not call a tool.
                - `'any'` or `'required'` or `True`: force at least one tool to be called.
                - `dict` of the form `{"type": "function", "function": {"name": <<tool_name>>}}`: calls `<<tool_name>>` tool.
                - `False` or `None`: no effect, default OpenAI behavior.
            strict: If `True`, model output is guaranteed to exactly match the JSON Schema
                provided in the tool definition. The input schema will also be validated according to the
                [supported schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas?api-mode=responses#supported-schemas).
                If `False`, input schema will not be validated and model output will not
                be validated. If `None`, `strict` argument will not be passed to the model.
            parallel_tool_calls: Set to `False` to disable parallel tool use.
                Defaults to `None` (no specification, which allows parallel tool use).
            response_format: Optional schema to format model response. If provided
                and the model does not call a tool, the model will generate a
                [structured response](https://platform.openai.com/docs/guides/structured-outputs).
            kwargs: Any additional parameters are passed directly to `bind`.
        Nr  r  r   rp   )r   r   r   anyrequiredzEUnrecognized tool_choice type. Expected str, bool or dict. Received: r  json_schemarq  r  ro  r  r   )rF   r   r   r   WellKnownToolsr   r   r   r`   r   "_convert_to_openai_response_formatr  bind)r.  r  r  r  r  ro  r  r   formatted_tools
tool_namesr   r  s              rd   
bind_toolszBaseChatOpenAI.bind_tools  s   L *,?F()DI
<@"47
 
 
# 	DT!!!$z"26":;4!!$v,/	 +s+*, *%+[$9#K !N2#);"7K !E)",KK.(K.!!,/  !o%$/F=!?D1#''/=@ 3 3M2 FF )7;;HdK"&t_]-KH-U"V(J)F$% w|</<V<<i
s   Efunction_callingmethodinclude_rawr  r  c          
     t   ||dk(  rd}t        |      t        |      }|dk(  r|r't        |t              rt	        j
                  d       d}| j                  rj| j                  j                  d      s*| j                  j                  d      s| j                  dk(  r%t	        j
                  d	| j                   d
       d}|dk(  rt|d}t        |      t        |      d   d   }	 | j                  d(i |	d|||d|dd|}
 | j                  |gfi |
}|rt        |gd      }n"t        |	d      }n|dk(  r7 | j                  d(i ddid|i|dd|}|rt        |      n	t               }n|dk(  r|d}t        |      t!        ||      }i t#        d(|||dt        |      dd|}
|r|D cg c]  }t        ||       c}|
d<    | j                  d(i |
}|rGt%        t'        t(        t+        t,        |                  j/                  t+        t,        |            }nt               }nd| d}t        |      |r^t1        j2                  t5        d       |z  d! "      }t1        j2                  d# $      }|j7                  |gd%&      }t9        |'      |z  S ||z  S c c}w ))a  Model wrapper that returns outputs formatted to match the given schema.

        Args:
            schema: The output schema. Can be passed in as:

                - An OpenAI function/tool schema,
                - A JSON Schema,
                - A `TypedDict` class,
                - Or a Pydantic class.

                If `schema` is a Pydantic class then the model output will be a
                Pydantic instance of that class, and the model-generated fields will be
                validated by the Pydantic class. Otherwise the model output will be a
                dict and will not be validated.

                See `langchain_core.utils.function_calling.convert_to_openai_tool` for
                more on how to properly specify types and descriptions of schema fields
                when specifying a Pydantic or `TypedDict` class.

            method: The method for steering model generation, one of:

                - `'function_calling'`:
                    Uses OpenAI's [tool-calling API](https://platform.openai.com/docs/guides/function-calling)
                    (formerly called function calling)
                - `'json_schema'`:
                    Uses OpenAI's [Structured Output API](https://platform.openai.com/docs/guides/structured-outputs)
                - `'json_mode'`:
                    Uses OpenAI's [JSON mode](https://platform.openai.com/docs/guides/structured-outputs/json-mode).
                    Note that if using JSON mode then you must include instructions for
                    formatting the output into the desired schema into the model call

            include_raw:
                If `False` then only the parsed structured output is returned.

                If an error occurs during model output parsing it will be raised.

                If `True` then both the raw model response (a `BaseMessage`) and the
                parsed model response will be returned.

                If an error occurs during output parsing it will be caught and returned
                as well.

                The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
                `'parsing_error'`.
            strict:

                - `True`:
                    Model output is guaranteed to exactly match the schema.
                    The input schema will also be validated according to the
                    [supported schemas](https://platform.openai.com/docs/guides/structured-outputs/supported-schemas?api-mode=responses#supported-schemas).
                - `False`:
                    Input schema will not be validated and model output will not be
                    validated.
                - `None`:
                    `strict` argument will not be passed to the model.

            tools:
                A list of tool-like objects to bind to the chat model. Requires that:

                - `method` is `'json_schema'` (default).
                - `strict=True`
                - `include_raw=True`

                If a model elects to call a tool, the resulting `AIMessage` in `'raw'`
                will include tool calls.

                ??? example

                    ```python
                    from langchain.chat_models import init_chat_model
                    from pydantic import BaseModel


                    class ResponseSchema(BaseModel):
                        response: str


                    def get_weather(location: str) -> str:
                        """Get weather at a location."""
                        pass

                    model = init_chat_model("openai:gpt-4o-mini")

                    structured_model = model.with_structured_output(
                        ResponseSchema,
                        tools=[get_weather],
                        strict=True,
                        include_raw=True,
                    )

                    structured_model.invoke("What's the weather in Boston?")
                    ```

                    ```python
                    {
                        "raw": AIMessage(content="", tool_calls=[...], ...),
                        "parsing_error": None,
                        "parsed": None,
                    }
                    ```

            kwargs: Additional keyword args are passed through to the model.

        Returns:
            A `Runnable` that takes same inputs as a
                `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
                `False` and `schema` is a Pydantic class, `Runnable` outputs an instance
                of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
                `False` then `Runnable` outputs a `dict`.

                If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:

                - `'raw'`: `BaseMessage`
                - `'parsed'`: `None` if there was a parsing error, otherwise the type
                    depends on the `schema` as described above.
                - `'parsing_error'`: `BaseException | None`

        !!! warning "Behavior changed in `langchain-openai` 0.3.12"

            Support for `tools` added.

        !!! warning "Behavior changed in `langchain-openai` 0.3.21"

            Pass `kwargs` through to the model.
        	json_modez<Argument `strict` is not supported with `method`='json_mode'r  zReceived a Pydantic BaseModel V1 schema. This is not supported by method="json_schema". Please use method="function_calling" or specify schema via JSON Schema or Pydantic V2 BaseModel. Overriding to method="function_calling".r	  zgpt-3zgpt-4-r  z+Cannot use method='json_schema' with model a   since it doesn't support OpenAI's Structured Output API. You can see supported models here: https://platform.openai.com/docs/guides/structured-outputs#supported-models. To fix this warning, set `method='function_calling'. Overriding to method='function_calling'.zGschema must be specified when method is not 'json_mode'. Received None.r   rp   F)r  r  )r  rq  )r  r  r  ls_structured_output_formatT)r  first_tool_only)key_namer  r   json_objectr  )ro  r  )pydantic_objectr  r  )rq  )output_typez\Unrecognized method argument. Expected one of 'function_calling' or 'json_mode'. Received: ''rawc                     y r^   r   r  s    rd   <lambda>z7BaseChatOpenAI.with_structured_output.<locals>.<lambda>x      rf   )r  parsing_errorc                     y r^   r   r  s    rd   r  z7BaseChatOpenAI.with_structured_output.<locals>.<lambda>z  r  rf   )r  r  )exception_key)r  r   )r   r  
issubclassBaseModelV1r   r   rb   r   rF   _filter_disabled_paramsr  r7   r6   r  r5   r4   r  r   r>   r
   _oai_structured_outputs_parserr   r   
with_typesr@   assignr   with_fallbacksr?   )r.  rq  r  r  r  r  r  r   is_pydantic_schema	tool_namebind_kwargsllmoutput_parserro  tparser_assignparser_noneparser_with_fallbacks                     rd   with_structured_outputz%BaseChatOpenAI.with_structured_outputy  sP   R &K"7PCS/!/7]" #z&+'F? ,**73??--h7??g-A$//AR S? ? ,''~%  !o%.v6zB6JI6$66 #,+0$-3v"F"(4		 	K "$//6(:k:C!*=!($(+
 !9&! {"$)) 	(.'>#+V"4"(4 	C & %V<%' 
 }$~%  !o%@PVWO	 $3-3v"F"8"@1 	K FK(AB*1V<(G$ $))*k*C! .:4fCUV!*dF);*<  !1 2++1(!5  S/!/66!%(=8M .44NKK#0#?#?_ $@ $  3'*>>>]""5(s   ?J5c                    | j                   s|S i }|j                         D ]9  \  }}|| j                   v r!| j                   |   || j                   |   v r5|||<   ; |S r^   )r	  r   )r.  r  filteredr   r   s        rd   r   z&BaseChatOpenAI._filter_disabled_params  sr    ##MLLN 	DAqD((($$Q'/18L8LQ8O3OHQK	 rf   c                D   | j                  |      }|j                  d   j                  }t        |t              r6|j
                  }d|j                  v r|j                  j                  d       nd}t        d|j                  |      }t        ||j                        S )zDGet chunk from completion (e.g., from final completion of a stream).r   rv   Nrs   )r-   ry   rZ  r[  )r  r  r   r   r   rZ  ry   r"  r   r;   r  )r.  
completionchat_resultchat_messagerZ  r   s         rd   r  z4BaseChatOpenAI._get_generation_chunk_from_completion  s     ..z:"..q199lI.)88N|===..22<@!N *<<)

 #[-C-C
 	
rf   )r  r   returnr   )r5  rR   r5  r   )rR  zlist[dict | None]r5  r   )rY  r   rd  r   re  dict | Noner5  zChatGenerationChunk | None)r5  None)NN)
r}  list[BaseMessage]r  r
  r~  CallbackManagerForLLMRun | Noner  r   r5  Iterator[ChatGenerationChunk])
r}  r9  r  r
  r~  $AsyncCallbackManagerForLLMRun | Noner  r   r5  "AsyncIterator[ChatGenerationChunk]r^   )r   r   r  r   r5  r   )r}  r9  r  r
  r~  r:  r   r   r  r   r5  r;  )
r}  r9  r  r
  r~  r:  r  r   r5  r<   r  r   r5  r   r  r   r  r
  r  r   r5  r   )r  zdict | openai.BaseModelr\  r7  r5  r<   )r}  r9  r  r
  r~  r<  r   r   r  r   r5  r=  )
r}  r9  r  r
  r~  r<  r  r   r5  r<   )r  r
  r  r   r5  r   )r  r
  r  r   r5  r   )r5  r   )r5  ztuple[str, tiktoken.Encoding])r{  r   r5  z	list[int])r}  Sequence[BaseMessage]r  z<Sequence[dict[str, Any] | type | Callable | BaseTool] | Noner5  r   )r  z5Sequence[dict[str, Any] | type | Callable | BaseTool]r  zdict | str | bool | Noner  r   r  r   ro  _DictOrPydanticClass | Noner  r   r5  z'Runnable[LanguageModelInput, AIMessage]rq  rA  r  z7Literal['function_calling', 'json_mode', 'json_schema']r  r   r  r   r  zlist | Noner  r   r5  z-Runnable[LanguageModelInput, _DictOrPydantic])r  r   r5  r   )r2  zopenai.BaseModelr5  r;   )W__name__
__module____qualname____doc__rO   r   __annotations__r   r   r   rb   r   r   r   rL   r   r   r   rK   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r	  r  r  r  r  r  r  r  rN   model_configrQ   classmethodr  r$  rE  rI  propertyrN  rU  ri  rk  r  r  r  r  r  r  ru  r  r  r  r  r  r  r  r  r  r  r  r.  r   r  __classcell__r  s   @rd   r   r     s   Dd3FC3dD9L#9T48K8"4>s>O7CJC $K$+#(#>L.>V 	9ISW)X	 K
-^ #(J"GOZG_&+D&OON  % > L*  AFIAO=  !%L+$ #K"<%)l)$&*|*;D* Hk %#L*# )-J%,PIt/AzAE<D"40J
0/#'j' (,I$+
 !Iz  '+* 15O-415M.5 $D$?K? %*$$EzE $)=M#ND
 N!+/J(/& &+d*X-24-@O*@  !%G$  $L*# E; "J
! &+d*< &*{) "' !4dC"NJ " t4L(#E  $E
 (#  $< '"z #zx '" # 
 
>2<
<
 "<
 *	<

 
$<
|" "&7;	4+#4+ 4+ 5	4+
 4+ 
'4+r "&<@	5+#5+ 5+ :	5+
 5+ 
,5+p +/*'*:=*	*, "&7;	E# %)E##E# E# 5	E# "E# E# 
'E#T "&7;	7C#7C 7C 5	7C
 7C 
7Cr+$ "&	" 	
  
D (,CJ)CJ %CJ 
	CJP "&<@	H# %)H##H# H# :	H# "H# H# 
,H#Z "&<@	<
#<
 <
 :	<

 <
 
<
| G G
 (,$7:	, (,$7:	&   + OS]'] L] 
	]F 15"+/7;\=D\= .	\=
 \= )\= 5\= \= 
1\=@ /3F# !"!F#+F#
	F# F# F# F# F# 
7F#P
*
	
rf   r   c                      e Zd ZU dZ edd      Zded<   	 edd       Ze	dd       Z
edd	       Ze	dd
       Zed fd       Zdd	 	 	 	 	 	 	 d fdZd fdZ	 	 	 	 	 	 d fdZ	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d fdZ xZS )
ChatOpenAIuc  Interface to OpenAI chat model APIs.

    ???+ info "Setup"

        Install `langchain-openai` and set environment variable `OPENAI_API_KEY`.

        ```bash
        pip install -U langchain-openai

        # or using uv
        uv add langchain-openai
        ```

        ```bash
        export OPENAI_API_KEY="your-api-key"
        ```

    ??? info "Key init args — completion params"

        | Param               | Type          | Description                                                                                                 |
        | ------------------- | ------------- | ----------------------------------------------------------------------------------------------------------- |
        | `model`             | `str`         | Name of OpenAI model to use.                                                                                |
        | `temperature`       | `float`       | Sampling temperature.                                                                                       |
        | `max_tokens`        | `int | None`  | Max number of tokens to generate.                                                                           |
        | `logprobs`          | `bool | None` | Whether to return logprobs.                                                                                 |
        | `stream_options`    | `dict`        | Configure streaming outputs, like whether to return token usage when streaming (`{"include_usage": True}`). |
        | `use_responses_api` | `bool | None` | Whether to use the responses API.                                                                           |

        See full list of supported init args and their descriptions below.

    ??? info "Key init args — client params"

        | Param          | Type                                       | Description                                                                         |
        | -------------- | ------------------------------------------ | ----------------------------------------------------------------------------------- |
        | `timeout`      | `float | Tuple[float, float] | Any | None` | Timeout for requests.                                                               |
        | `max_retries`  | `int | None`                               | Max number of retries.                                                              |
        | `api_key`      | `str | None`                               | OpenAI API key. If not passed in will be read from env var `OPENAI_API_KEY`.        |
        | `base_url`     | `str | None`                               | Base URL for API requests. Only specify if using a proxy or service emulator.       |
        | `organization` | `str | None`                               | OpenAI organization ID. If not passed in will be read from env var `OPENAI_ORG_ID`. |

        See full list of supported init args and their descriptions below.

    ??? info "Instantiate"

        Create a model instance with desired params. For example:

        ```python
        from langchain_openai import ChatOpenAI

        model = ChatOpenAI(
            model="...",
            temperature=0,
            max_tokens=None,
            timeout=None,
            max_retries=2,
            # api_key="...",
            # base_url="...",
            # organization="...",
            # other params...
        )
        ```

        See all available params below.

        !!! tip "Preserved params"
            Any param which is not explicitly supported will be passed directly to
            [`openai.OpenAI.chat.completions.create(...)`](https://platform.openai.com/docs/api-reference/chat/create)
            every time to the model is invoked. For example:

            ```python
            from langchain_openai import ChatOpenAI
            import openai

            ChatOpenAI(..., frequency_penalty=0.2).invoke(...)

            # Results in underlying API call of:

            openai.OpenAI(..).chat.completions.create(..., frequency_penalty=0.2)

            # Which is also equivalent to:

            ChatOpenAI(...).invoke(..., frequency_penalty=0.2)
            ```

    ??? info "Invoke"

        Generate a response from the model:

        ```python
        messages = [
            (
                "system",
                "You are a helpful translator. Translate the user sentence to French.",
            ),
            ("human", "I love programming."),
        ]
        model.invoke(messages)
        ```

        Results in an `AIMessage` response:

        ```python
        AIMessage(
            content="J'adore la programmation.",
            response_metadata={
                "token_usage": {
                    "completion_tokens": 5,
                    "prompt_tokens": 31,
                    "total_tokens": 36,
                },
                "model_name": "gpt-4o",
                "system_fingerprint": "fp_43dfabdef1",
                "finish_reason": "stop",
                "logprobs": None,
            },
            id="run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0",
            usage_metadata={"input_tokens": 31, "output_tokens": 5, "total_tokens": 36},
        )
        ```

    ??? info "Stream"

        Stream a response from the model:

        ```python
        for chunk in model.stream(messages):
            print(chunk.text, end="")
        ```

        Results in a sequence of `AIMessageChunk` objects with partial content:

        ```python
        AIMessageChunk(content="", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
        AIMessageChunk(content="J", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
        AIMessageChunk(content="'adore", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
        AIMessageChunk(content=" la", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
        AIMessageChunk(
            content=" programmation", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0"
        )
        AIMessageChunk(content=".", id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0")
        AIMessageChunk(
            content="",
            response_metadata={"finish_reason": "stop"},
            id="run-9e1517e3-12bf-48f2-bb1b-2e824f7cd7b0",
        )
        ```

        To collect the full message, you can concatenate the chunks:

        ```python
        stream = model.stream(messages)
        full = next(stream)
        for chunk in stream:
            full += chunk
        ```

        ```python
        full = AIMessageChunk(
            content="J'adore la programmation.",
            response_metadata={"finish_reason": "stop"},
            id="run-bf917526-7f58-4683-84f7-36a6b671d140",
        )
        ```

    ??? info "Async"

        Asynchronous equivalents of `invoke`, `stream`, and `batch` are also available:

        ```python
        # Invoke
        await model.ainvoke(messages)

        # Stream
        async for chunk in (await model.astream(messages))

        # Batch
        await model.abatch([messages])
        ```

        Results in an `AIMessage` response:

        ```python
        AIMessage(
            content="J'adore la programmation.",
            response_metadata={
                "token_usage": {
                    "completion_tokens": 5,
                    "prompt_tokens": 31,
                    "total_tokens": 36,
                },
                "model_name": "gpt-4o",
                "system_fingerprint": "fp_43dfabdef1",
                "finish_reason": "stop",
                "logprobs": None,
            },
            id="run-012cffe2-5d3d-424d-83b5-51c6d4a593d1-0",
            usage_metadata={
                "input_tokens": 31,
                "output_tokens": 5,
                "total_tokens": 36,
            },
        )
        ```

        For batched calls, results in a `list[AIMessage]`.

    ??? info "Tool calling"

        ```python
        from pydantic import BaseModel, Field


        class GetWeather(BaseModel):
            '''Get the current weather in a given location'''

            location: str = Field(
                ..., description="The city and state, e.g. San Francisco, CA"
            )


        class GetPopulation(BaseModel):
            '''Get the current population in a given location'''

            location: str = Field(
                ..., description="The city and state, e.g. San Francisco, CA"
            )


        model_with_tools = model.bind_tools(
            [GetWeather, GetPopulation]
            # strict = True  # Enforce tool args schema is respected
        )
        ai_msg = model_with_tools.invoke(
            "Which city is hotter today and which is bigger: LA or NY?"
        )
        ai_msg.tool_calls
        ```

        ```python
        [
            {
                "name": "GetWeather",
                "args": {"location": "Los Angeles, CA"},
                "id": "call_6XswGD5Pqk8Tt5atYr7tfenU",
            },
            {
                "name": "GetWeather",
                "args": {"location": "New York, NY"},
                "id": "call_ZVL15vA8Y7kXqOy3dtmQgeCi",
            },
            {
                "name": "GetPopulation",
                "args": {"location": "Los Angeles, CA"},
                "id": "call_49CFW8zqC9W7mh7hbMLSIrXw",
            },
            {
                "name": "GetPopulation",
                "args": {"location": "New York, NY"},
                "id": "call_6ghfKxV264jEfe1mRIkS3PE7",
            },
        ]
        ```

        !!! note "Parallel tool calls"
            [`openai >= 1.32`](https://pypi.org/project/openai/) supports a
            `parallel_tool_calls` parameter that defaults to `True`. This parameter can
            be set to `False` to disable parallel tool calls:

            ```python
            ai_msg = model_with_tools.invoke(
                "What is the weather in LA and NY?", parallel_tool_calls=False
            )
            ai_msg.tool_calls
            ```

            ```python
            [
                {
                    "name": "GetWeather",
                    "args": {"location": "Los Angeles, CA"},
                    "id": "call_4OoY0ZR99iEvC7fevsH8Uhtz",
                }
            ]
            ```

        Like other runtime parameters, `parallel_tool_calls` can be bound to a model
        using `model.bind(parallel_tool_calls=False)` or during instantiation by
        setting `model_kwargs`.

        See `bind_tools` for more.

    ??? info "Built-in (server-side) tools"

        You can access [built-in tools](https://platform.openai.com/docs/guides/tools?api-mode=responses)
        supported by the OpenAI Responses API. See [LangChain docs](https://docs.langchain.com/oss/python/integrations/chat/openai#responses-api)
        for more detail.

        ```python
        from langchain_openai import ChatOpenAI

        model = ChatOpenAI(model="...", output_version="responses/v1")

        tool = {"type": "web_search"}
        model_with_tools = model.bind_tools([tool])

        response = model_with_tools.invoke("What was a positive news story from today?")
        response.content
        ```

        ```python
        [
            {
                "type": "text",
                "text": "Today, a heartwarming story emerged from ...",
                "annotations": [
                    {
                        "end_index": 778,
                        "start_index": 682,
                        "title": "Title of story",
                        "type": "url_citation",
                        "url": "<url of story>",
                    }
                ],
            }
        ]
        ```

        !!! version-added "Added in `langchain-openai` 0.3.9"

        !!! version-added "Added in `langchain-openai` 0.3.26: Updated `AIMessage` format"
            [`langchain-openai >= 0.3.26`](https://pypi.org/project/langchain-openai/#history)
            allows users to opt-in to an updated `AIMessage` format when using the
            Responses API. Setting `ChatOpenAI(..., output_version="responses/v1")` will
            format output from reasoning summaries, built-in tool invocations, and other
            response items into the message's `content` field, rather than
            `additional_kwargs`. We recommend this format for new applications.

    ??? info "Managing conversation state"

        OpenAI's Responses API supports management of [conversation state](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses).
        Passing in response IDs from previous messages will continue a conversational
        thread.

        ```python
        from langchain_openai import ChatOpenAI

        model = ChatOpenAI(
            model="...",
            use_responses_api=True,
            output_version="responses/v1",
        )
        response = model.invoke("Hi, I'm Bob.")
        response.text
        ```

        ```txt
        "Hi Bob! How can I assist you today?"
        ```

        ```python
        second_response = model.invoke(
            "What is my name?",
            previous_response_id=response.response_metadata["id"],
        )
        second_response.text
        ```

        ```txt
        "Your name is Bob. How can I help you today, Bob?"
        ```

        !!! version-added "Added in `langchain-openai` 0.3.9"

        !!! version-added "Added in `langchain-openai` 0.3.26"
            You can also initialize `ChatOpenAI` with `use_previous_response_id`.
            Input messages up to the most recent response will then be dropped from request
            payloads, and `previous_response_id` will be set using the ID of the most
            recent response.

            ```python
            model = ChatOpenAI(model="...", use_previous_response_id=True)
            ```

    ??? info "Reasoning output"

        OpenAI's Responses API supports [reasoning models](https://platform.openai.com/docs/guides/reasoning?api-mode=responses)
        that expose a summary of internal reasoning processes.

        ```python
        from langchain_openai import ChatOpenAI

        reasoning = {
            "effort": "medium",  # 'low', 'medium', or 'high'
            "summary": "auto",  # 'detailed', 'auto', or None
        }

        model = ChatOpenAI(
            model="...", reasoning=reasoning, output_version="responses/v1"
        )
        response = model.invoke("What is 3^3?")

        # Response text
        print(f"Output: {response.text}")

        # Reasoning summaries
        for block in response.content:
            if block["type"] == "reasoning":
                for summary in block["summary"]:
                    print(summary["text"])
        ```

        ```txt
        Output: 3³ = 27
        Reasoning: The user wants to know...
        ```

        !!! version-added "Added in `langchain-openai` 0.3.26: Updated `AIMessage` format"
            [`langchain-openai >= 0.3.26`](https://pypi.org/project/langchain-openai/#history)
            allows users to opt-in to an updated `AIMessage` format when using the
            Responses API. Setting `ChatOpenAI(..., output_version="responses/v1")` will
            format output from reasoning summaries, built-in tool invocations, and other
            response items into the message's `content` field, rather than
            `additional_kwargs`. We recommend this format for new applications.

    ??? info "Structured output"

        ```python
        from pydantic import BaseModel, Field


        class Joke(BaseModel):
            '''Joke to tell user.'''

            setup: str = Field(description="The setup of the joke")
            punchline: str = Field(description="The punchline to the joke")
            rating: int | None = Field(
                description="How funny the joke is, from 1 to 10"
            )


        structured_model = model.with_structured_output(Joke)
        structured_model.invoke("Tell me a joke about cats")
        ```

        ```python
        Joke(
            setup="Why was the cat sitting on the computer?",
            punchline="To keep an eye on the mouse!",
            rating=None,
        )
        ```

        See `with_structured_output` for more info.

    ??? info "JSON mode"

        ```python
        json_model = model.bind(response_format={"type": "json_object"})
        ai_msg = json_model.invoke(
            "Return a JSON object with key 'random_ints' and a value of 10 random ints in [0-99]"
        )
        ai_msg.content
        ```

        ```txt
        '\\n{\\n  "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\\n}'
        ```

    ??? info "Image input"

        ```python
        import base64
        import httpx
        from langchain.messages import HumanMessage

        image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg"
        image_data = base64.b64encode(httpx.get(image_url).content).decode("utf-8")
        message = HumanMessage(
            content=[
                {"type": "text", "text": "describe the weather in this image"},
                {
                    "type": "image_url",
                    "image_url": {"url": f"data:image/jpeg;base64,{image_data}"},
                },
            ]
        )

        ai_msg = model.invoke([message])
        ai_msg.content
        ```

        ```txt
        "The weather in the image appears to be clear and pleasant. The sky is mostly blue with scattered, light clouds, suggesting a sunny day with minimal cloud cover. There is no indication of rain or strong winds, and the overall scene looks bright and calm. The lush green grass and clear visibility further indicate good weather conditions."
        ```

    ??? info "Token usage"

        ```python
        ai_msg = model.invoke(messages)
        ai_msg.usage_metadata

        ```txt
        {"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}
        ```

        When streaming, set the `stream_usage` kwarg:

        ```python
        stream = model.stream(messages, stream_usage=True)
        full = next(stream)
        for chunk in stream:
            full += chunk
        full.usage_metadata
        ```

        ```txt
        {"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}
        ```

    ??? info "Logprobs"

        ```python
        logprobs_model = model.bind(logprobs=True)
        ai_msg = logprobs_model.invoke(messages)
        ai_msg.response_metadata["logprobs"]
        ```

        ```txt
        {
            "content": [
                {
                    "token": "J",
                    "bytes": [74],
                    "logprob": -4.9617593e-06,
                    "top_logprobs": [],
                },
                {
                    "token": "'adore",
                    "bytes": [39, 97, 100, 111, 114, 101],
                    "logprob": -0.25202933,
                    "top_logprobs": [],
                },
                {
                    "token": " la",
                    "bytes": [32, 108, 97],
                    "logprob": -0.20141791,
                    "top_logprobs": [],
                },
                {
                    "token": " programmation",
                    "bytes": [
                        32,
                        112,
                        114,
                        111,
                        103,
                        114,
                        97,
                        109,
                        109,
                        97,
                        116,
                        105,
                        111,
                        110,
                    ],
                    "logprob": -1.9361265e-07,
                    "top_logprobs": [],
                },
                {
                    "token": ".",
                    "bytes": [46],
                    "logprob": -1.2233183e-05,
                    "top_logprobs": [],
                },
            ]
        }
        ```

    ??? info "Response metadata"

        ```python
        ai_msg = model.invoke(messages)
        ai_msg.response_metadata
        ```

        ```txt
        {
            "token_usage": {
                "completion_tokens": 5,
                "prompt_tokens": 28,
                "total_tokens": 33,
            },
            "model_name": "gpt-4o",
            "system_fingerprint": "fp_319be4768e",
            "finish_reason": "stop",
            "logprobs": None,
        }
        ```

    ??? info "Flex processing"

        OpenAI offers a variety of [service tiers](https://platform.openai.com/docs/guides/flex-processing?api-mode=responses).
        The "flex" tier offers cheaper pricing for requests, with the trade-off that
        responses may take longer and resources might not always be available.
        This approach is best suited for non-critical tasks, including model testing,
        data enhancement, or jobs that can be run asynchronously.

        To use it, initialize the model with `service_tier="flex"`:

        ```python
        from langchain_openai import ChatOpenAI

        model = ChatOpenAI(model="...", service_tier="flex")
        ```

        Note that this is a beta feature that is only available for a subset of models.
        See OpenAI [flex processing docs](https://platform.openai.com/docs/guides/flex-processing?api-mode=responses)
        for more detail.

    ??? info "OpenAI-compatible APIs"

        `ChatOpenAI` can be used with OpenAI-compatible APIs like
        [LM Studio](https://lmstudio.ai/), [vLLM](https://github.com/vllm-project/vllm),
        [Ollama](https://ollama.com/), and others.

        To use custom parameters specific to these providers, use the `extra_body` parameter.

        !!! example "LM Studio example with TTL (auto-eviction)"

            ```python
            from langchain_openai import ChatOpenAI

            model = ChatOpenAI(
                base_url="http://localhost:1234/v1",
                api_key="lm-studio",  # Can be any string
                model="mlx-community/QwQ-32B-4bit",
                temperature=0,
                extra_body={
                    "ttl": 300
                },  # Auto-evict model after 5 minutes of inactivity
            )
            ```

        !!! example "vLLM example with custom parameters"

            ```python
            model = ChatOpenAI(
                base_url="http://localhost:8000/v1",
                api_key="EMPTY",
                model="meta-llama/Llama-2-7b-chat-hf",
                extra_body={"use_beam_search": True, "best_of": 4},
            )
            ```

    ??? info "`model_kwargs` vs `extra_body`"

        Use the correct parameter for different types of API arguments:

        **Use `model_kwargs` for:**

        - Standard OpenAI API parameters not explicitly defined as class parameters
        - Parameters that should be flattened into the top-level request payload
        - Examples: `max_completion_tokens`, `stream_options`, `modalities`, `audio`

        ```python
        # Standard OpenAI parameters
        model = ChatOpenAI(
            model="...",
            model_kwargs={
                "stream_options": {"include_usage": True},
                "max_completion_tokens": 300,
                "modalities": ["text", "audio"],
                "audio": {"voice": "alloy", "format": "wav"},
            },
        )
        ```

        **Use `extra_body` for:**

        - Custom parameters specific to OpenAI-compatible providers (vLLM, LM Studio,
            OpenRouter, etc.)
        - Parameters that need to be nested under `extra_body` in the request
        - Any non-standard OpenAI API parameters

        ```python
        # Custom provider parameters
        model = ChatOpenAI(
            base_url="http://localhost:8000/v1",
            model="custom-model",
            extra_body={
                "use_beam_search": True,  # vLLM parameter
                "best_of": 4,  # vLLM parameter
                "ttl": 300,  # LM Studio parameter
            },
        )
        ```

        **Key Differences:**

        - `model_kwargs`: Parameters are **merged into top-level** request payload
        - `extra_body`: Parameters are **nested under `extra_body`** key in request

        !!! warning
            Always use `extra_body` for custom parameters, **not** `model_kwargs`.
            Using `model_kwargs` for non-OpenAI parameters will cause API errors.

    ??? info "Prompt caching optimization"

        For high-volume applications with repetitive prompts, use `prompt_cache_key`
        per-invocation to improve cache hit rates and reduce costs:

        ```python
        model = ChatOpenAI(model="...")

        response = model.invoke(
            messages,
            prompt_cache_key="example-key-a",  # Routes to same machine for cache hits
        )

        customer_response = model.invoke(messages, prompt_cache_key="example-key-b")
        support_response = model.invoke(messages, prompt_cache_key="example-key-c")

        # Dynamic cache keys based on context
        cache_key = f"example-key-{dynamic_suffix}"
        response = model.invoke(messages, prompt_cache_key=cache_key)
        ```

        Cache keys help ensure requests with the same prompt prefix are routed to
        machines with existing cache, providing cost reduction and latency improvement on
        cached tokens.
    Nr  r   r   r   c                
    ddiS )z(Mapping of secret environment variables.r   r   r   rH  s    rd   
lc_secretszChatOpenAI.lc_secrets  s     !"233rf   c                
    g dS )zzGet the namespace of the LangChain object.

        Returns:
            `["langchain", "chat_models", "openai"]`
        )	langchainchat_modelsr;  r   r  s    rd   get_lc_namespacezChatOpenAI.get_lc_namespace  s
     65rf   c                    i }| j                   r| j                   |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   |S )z+Get the attributes of the langchain object.r   r   r   )r   r   r   )r.  
attributess     rd   lc_attributeszChatOpenAI.lc_attributes  s\     &(
##040H0HJ,-,0,@,@J())-):):J~&rf   c                     y)z9Return whether this model can be serialized by LangChain.Tr   rT  s    rd   is_lc_serializablezChatOpenAI.is_lc_serializable  s     rf   c                L    t         |   }d|v r|j                  d      |d<   |S )rK  r   r  )r  rN  r"  )r.  r  r  s     rd   rN  zChatOpenAI._default_params  s0     (6!.4jj.FF*+rf   r  c                   t        |   |fd|i|}d|v r|j                  d      |d<   | j                  rEt	        j
                  d| j                        r%|j                  dg       D ]  }|d   dk(  sd|d<    |S )	Nr  r   r  z^o\dr}  ro   r|   r}   )r  ru  r"  rb   rematchr`   )r.  r  r  r  r  r   r  s         rd   ru  zChatOpenAI._get_request_payload  s     '.vKDKFK 7"/6{{</HG+, ??rxxA";;z26 26?h.&1GFO2 rf   c                ~    | j                  i || j                        rt        |   |i |S t        |   |i |S )+Route to Chat Completions or Responses API.)r  r   r  r  r  )r.  r   r  r  s      rd   r  zChatOpenAI._stream  sK    ""#Bf#B0A0A#BC7,d=f==w///rf   c                  K   | j                  i || j                        rt        |   |i |2 3 d{   }| t        |   |i |2 3 d{   }| 7 (6 y7 6 yw)r`  N)r  r   r  r  r  )r.  r   r  rY  r  s       rd   r  zChatOpenAI._astream  s      ""#Bf#B0A0A#BC$w94J6J  e$w/@@  e	J@sI   /A'A!AA!A'A%A#A%A'A!!A'#A%%A'r  Fr
  c               .    t        |   |f||||d|S )a>  Model wrapper that returns outputs formatted to match the given schema.

        Args:
            schema: The output schema. Can be passed in as:

                - an OpenAI function/tool schema,
                - a JSON Schema,
                - a `TypedDict` class,
                - or a Pydantic class.

                If `schema` is a Pydantic class then the model output will be a
                Pydantic instance of that class, and the model-generated fields will be
                validated by the Pydantic class. Otherwise the model output will be a
                dict and will not be validated.

                See `langchain_core.utils.function_calling.convert_to_openai_tool` for
                more on how to properly specify types and descriptions of schema fields
                when specifying a Pydantic or `TypedDict` class.

            method: The method for steering model generation, one of:

                - `'json_schema'`:
                    Uses OpenAI's [Structured Output API](https://platform.openai.com/docs/guides/structured-outputs).
                    See the docs for [supported models](https://platform.openai.com/docs/guides/structured-outputs#supported-models).
                - `'function_calling'`:
                    Uses OpenAI's [tool-calling API](https://platform.openai.com/docs/guides/function-calling)
                    (formerly called function calling).
                - `'json_mode'`:
                    Uses OpenAI's [JSON mode](https://platform.openai.com/docs/guides/structured-outputs#json-mode).
                    Note that if using JSON mode then you must include instructions for
                    formatting the output into the desired schema into the model call.

                Learn more about the [differences between methods](https://platform.openai.com/docs/guides/structured-outputs#function-calling-vs-response-format).

            include_raw:
                If `False` then only the parsed structured output is returned.

                If an error occurs during model output parsing it will be raised.

                If `True` then both the raw model response (a `BaseMessage`) and the
                parsed model response will be returned.

                If an error occurs during output parsing it will be caught and returned
                as well.

                The final output is always a `dict` with keys `'raw'`, `'parsed'`, and
                `'parsing_error'`.
            strict:

                - `True`:
                    Model output is guaranteed to exactly match the schema.
                    The input schema will also be validated according to the
                    [supported schemas](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas).
                - `False`:
                    Input schema will not be validated and model output will not be
                    validated.
                - `None`:
                    `strict` argument will not be passed to the model.

                If schema is specified via `TypedDict` or JSON schema, `strict` is not
                enabled by default. Pass `strict=True` to enable it.

                !!! note
                    `strict` can only be non-null if `method` is `'json_schema'` or `'function_calling'`.
            tools:
                A list of tool-like objects to bind to the chat model. Requires that:

                - `method` is `'json_schema'` (default).
                - `strict=True`
                - `include_raw=True`

                If a model elects to call a
                tool, the resulting `AIMessage` in `'raw'` will include tool calls.

                ??? example

                    ```python
                    from langchain.chat_models import init_chat_model
                    from pydantic import BaseModel


                    class ResponseSchema(BaseModel):
                        response: str


                    def get_weather(location: str) -> str:
                        \"\"\"Get weather at a location.\"\"\"
                        pass

                    model = init_chat_model("openai:gpt-4o-mini")

                    structured_model = model.with_structured_output(
                        ResponseSchema,
                        tools=[get_weather],
                        strict=True,
                        include_raw=True,
                    )

                    structured_model.invoke("What's the weather in Boston?")
                    ```

                    ```python
                    {
                        "raw": AIMessage(content="", tool_calls=[...], ...),
                        "parsing_error": None,
                        "parsed": None,
                    }
                    ```

            kwargs: Additional keyword args are passed through to the model.

        Returns:
            A `Runnable` that takes same inputs as a
                `langchain_core.language_models.chat.BaseChatModel`. If `include_raw` is
                `False` and `schema` is a Pydantic class, `Runnable` outputs an instance
                of `schema` (i.e., a Pydantic object). Otherwise, if `include_raw` is
                `False` then `Runnable` outputs a `dict`.

                If `include_raw` is `True`, then `Runnable` outputs a `dict` with keys:

                - `'raw'`: `BaseMessage`
                - `'parsed'`: `None` if there was a parsing error, otherwise the type
                    depends on the `schema` as described above.
                - `'parsing_error'`: `BaseException | None`

        !!! warning "Behavior changed in `langchain-openai` 0.3.0"

            `method` default changed from `"function_calling"` to `"json_schema"`.

        !!! warning "Behavior changed in `langchain-openai` 0.3.12"

            Support for `tools` added.

        !!! warning "Behavior changed in `langchain-openai` 0.3.21"

            Pass `kwargs` through to the model.

        ??? note "Example: `schema=Pydantic` class, `method='json_schema'`, `include_raw=False`, `strict=True`"

            Note, OpenAI has a number of restrictions on what types of schemas can be
            provided if `strict = True`. When using Pydantic, our model cannot
            specify any Field metadata (like min/max constraints) and fields cannot
            have default values.

            See [all constraints](https://platform.openai.com/docs/guides/structured-outputs#supported-schemas).

            ```python
            from langchain_openai import ChatOpenAI
            from pydantic import BaseModel, Field


            class AnswerWithJustification(BaseModel):
                '''An answer to the user question along with justification for the answer.'''

                answer: str
                justification: str | None = Field(
                    default=..., description="A justification for the answer."
                )


            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(AnswerWithJustification)

            structured_model.invoke(
                "What weighs more a pound of bricks or a pound of feathers"
            )
            ```

            ```python
            AnswerWithJustification(
                answer="They weigh the same",
                justification="Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.",
            )
            ```

        ??? note "Example: `schema=Pydantic` class, `method='function_calling'`, `include_raw=False`, `strict=False`"

            ```python
            from langchain_openai import ChatOpenAI
            from pydantic import BaseModel, Field


            class AnswerWithJustification(BaseModel):
                '''An answer to the user question along with justification for the answer.'''

                answer: str
                justification: str | None = Field(
                    default=..., description="A justification for the answer."
                )


            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(
                AnswerWithJustification, method="function_calling"
            )

            structured_model.invoke(
                "What weighs more a pound of bricks or a pound of feathers"
            )
            ```

            ```python
            AnswerWithJustification(
                answer="They weigh the same",
                justification="Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.",
            )
            ```

        ??? note "Example: `schema=Pydantic` class, `method='json_schema'`, `include_raw=True`"

            ```python
            from langchain_openai import ChatOpenAI
            from pydantic import BaseModel


            class AnswerWithJustification(BaseModel):
                '''An answer to the user question along with justification for the answer.'''

                answer: str
                justification: str


            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(
                AnswerWithJustification, include_raw=True
            )

            structured_model.invoke(
                "What weighs more a pound of bricks or a pound of feathers"
            )
            ```

            ```python
            {
                "raw": AIMessage(
                    content="",
                    additional_kwargs={
                        "tool_calls": [
                            {
                                "id": "call_Ao02pnFYXD6GN1yzc0uXPsvF",
                                "function": {
                                    "arguments": '{"answer":"They weigh the same.","justification":"Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ."}',
                                    "name": "AnswerWithJustification",
                                },
                                "type": "function",
                            }
                        ]
                    },
                ),
                "parsed": AnswerWithJustification(
                    answer="They weigh the same.",
                    justification="Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume or density of the objects may differ.",
                ),
                "parsing_error": None,
            }
            ```

        ??? note "Example: `schema=TypedDict` class, `method='json_schema'`, `include_raw=False`, `strict=False`"

            ```python
            from typing_extensions import Annotated, TypedDict

            from langchain_openai import ChatOpenAI


            class AnswerWithJustification(TypedDict):
                '''An answer to the user question along with justification for the answer.'''

                answer: str
                justification: Annotated[
                    str | None, None, "A justification for the answer."
                ]


            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(AnswerWithJustification)

            structured_model.invoke(
                "What weighs more a pound of bricks or a pound of feathers"
            )
            ```

            ```python
            {
                "answer": "They weigh the same",
                "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.",
            }
            ```

        ??? note "Example: `schema=OpenAI` function schema, `method='json_schema'`, `include_raw=False`"

            ```python
            from langchain_openai import ChatOpenAI

            oai_schema = {
                "name": "AnswerWithJustification",
                "description": "An answer to the user question along with justification for the answer.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "answer": {"type": "string"},
                        "justification": {
                            "description": "A justification for the answer.",
                            "type": "string",
                        },
                    },
                    "required": ["answer"],
                },
            }

            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(oai_schema)

            structured_model.invoke(
                "What weighs more a pound of bricks or a pound of feathers"
            )
            ```

            ```python
            {
                "answer": "They weigh the same",
                "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.",
            }
            ```

        ??? note "Example: `schema=Pydantic` class, `method='json_mode'`, `include_raw=True`"

            ```python
            from langchain_openai import ChatOpenAI
            from pydantic import BaseModel


            class AnswerWithJustification(BaseModel):
                answer: str
                justification: str


            model = ChatOpenAI(model="...", temperature=0)
            structured_model = model.with_structured_output(
                AnswerWithJustification, method="json_mode", include_raw=True
            )

            structured_model.invoke(
                "Answer the following question. "
                "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
                "What's heavier a pound of bricks or a pound of feathers?"
            )
            ```

            ```python
            {
                "raw": AIMessage(
                    content='{\\n    "answer": "They are both the same weight.",\\n    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'
                ),
                "parsed": AnswerWithJustification(
                    answer="They are both the same weight.",
                    justification="Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.",
                ),
                "parsing_error": None,
            }
            ```

        ??? note "Example: `schema=None`, `method='json_mode'`, `include_raw=True`"

            ```python
            structured_model = model.with_structured_output(
                method="json_mode", include_raw=True
            )

            structured_model.invoke(
                "Answer the following question. "
                "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
                "What's heavier a pound of bricks or a pound of feathers?"
            )
            ```

            ```python
            {
                "raw": AIMessage(
                    content='{\\n    "answer": "They are both the same weight.",\\n    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'
                ),
                "parsed": {
                    "answer": "They are both the same weight.",
                    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.",
                },
                "parsing_error": None,
            }
            ```

        r
  )r  r.  )r.  rq  r  r  r  r  r  r  s          rd   r.  z!ChatOpenAI.with_structured_output  s6    ` w-
#
 
 	
rf   )r5  zdict[str, str])r5  z	list[str]r6  )r5  r   r?  )r   r   r  r   r5  r;  )r   r   r  r   r5  r=  r^   rB  )rC  rD  rE  rF  rO   r   rG  rJ  rP  rI  rU  rX  rZ  rN  ru  r  r  r.  rK  rL  s   @rd   rN  rN    sH   [z #47NOJ
O/4 4 6 6       "&	" 	
  
(0		$'		+	 /3W
 KX!"!W
+W
 H	W

 W
 W
 W
 W
 
7W
 W
rf   rN  c                <    t        | t              xr t        |       S r^   )r   r   rI   )objs    rd   r  r  r  s    c4 ?%:3%??rf   c                P    d| d   | d   t        j                  | d   d      ddS )	Nr   rq   rp   r   F)ensure_asciirp   r   r   rq   r   )jsondumps)r   s    rd   r   r   v  s6    of%If$5EJ
 rf   c                &    d| d   | d   | d   ddS )Nr   rq   rp   r   rg  rh  r   )invalid_tool_calls    rd   r   r     s.     %%f-*62
 rf   c                .   	 ddl m} t        |       r[	 dd l}|j                  |       }|j                          |j                  t        |j                              j                  \  }}||fS t        |       rU| j                  dd      \  }}t        j                   |      }|j                  t        |            j                  \  }}||fS y # t        $ r t        j	                  d       Y y w xY w# t        $ r t        j	                  d       Y y w xY w)Nr   )ImagezaUnable to count image tokens. To count image tokens please install `pip install -U pillow httpx`.zZUnable to count image tokens. To count image tokens please install `pip install -U httpx`.,r  )PILrn  r8  loggerinfo_is_urlr7  r`   raise_for_statusopenr   r-   size_is_b64splitr   	b64decode)	image_sourcern  r7  r  widthheightr  encodedr   s	            rd   r  r    s	    |	 99\*!!#

78+;+;#<=BBvf}|!''Q/
7(

74=166vf}1  -	
   	KK* 	s"   C C3 C0/C03DDc                n    t        | |      \  } }t        |dz        }t        | dz        }d|z  |z  dz   S )Ni      r  )_resizer   )r{  r|  hws       rd   r  r    sA    E6*ME6Vc\AUS[A!GaK2rf   c                    	 t        |       }t        |j                  |j                  g      S # t        $ r }t
        j                  d|       Y d }~yd }~ww xY w)NzUnable to parse URL: %sF)r   r5  schemenetlocr   rq  debug)sresultr   s      rd   rs  rs    sJ    !FMM6==122 .2s   +. 	AAAc                $    | j                  d      S )Nz
data:image)r   )r  s    rd   rw  rw    s    <<%%rf   c                    | dkD  s|dkD  r| |kD  r|dz  | z  }d} n
| dz  |z  } d}| dkD  r"|dkD  r| |kD  r| dz  |z  } d}| |fS |dz  | z  }d} | |fS )Ni   i   r   )r{  r|  s     rd   r  r    s    t|v}6>tm-FET\f,EFs{v|6>S[V+EF &= slu,FE&=rf   r  c               <   t        | t              rt        |       r| S t        | t              rd| v r| j	                  d      dk(  r| }n}t        | t              rd| v r
d| v rd| d}n_|7t        | t              r%t        | j	                  d      t
              r| d   }nd}t        | |      }|j                  d	      |d<   d|d}|T||d   j	                  d      ur>t        | t              r.d| j	                  di       v rd
| d   d    d| d}t        |      |S )Nr  r   rp   rq  )r   r  r  Fr  
parametersz0Output schema already has 'strict' value set to z: but 'strict' also passed in to with_structured_output as z@. Please make sure that 'strict' is only specified in one place.)	r   r   rI   r   r`   r   rE   r"  r   )rq  r  ro  r   r   s        rd   r  r    s=    &$$9&$A 	64 V#JJv-/ 	FD	!f&68v;M#0H>&$'Jvzz(7KT,R)-fVD%\\,7#0J 	/-8<<XFFvt$

="55 ?m$X./ 0))/ 178 	 orf   c                   | j                   j                  d      x}rt        |t              r |di |S |S | j                   j                  d      rt	        | j                   d         t        d | j                  D              r't        d | j                  D              }t	        |      | j                  ry d|  }t        |      )Nr  r  c              3  z   K   | ]3  }t        |t              xr |j                  d       dk(  xr d|d   v  5 ywr   non_standardr  r  Nr   r   r`   r,  r   s     rd   r/  z1_oai_structured_outputs_parser.<locals>.<genexpr>  sM        	5$ 	(IIf/	(w'	(s   9;c              3  j   K   | ]+  }t        |t              r|d    dk(  rd|d   v r
|d   d    - ywr  r   r   r  s     rd   r/  z1_oai_structured_outputs_parser.<locals>.<genexpr>  sC      
%&f/U7^+	 'N9%
s   13zdStructured Output response does not have a 'parsed' field nor a 'refusal' field. Received message:

r   )
ry   r`   r   r   OpenAIRefusalErrorr   content_blocksnextrv   r   )ai_msgrq  r  r  r   s        rd   r!  r!    s     ))--h77v7fd##F####I. !9!9)!DEE
  **	   
..
 
 !))	''-h	0  S/rf   c                      e Zd ZdZy)r  aE  Error raised when OpenAI Structured Outputs API returns a refusal.

    When using OpenAI's Structured Outputs API with user-generated input, the model
    may occasionally refuse to fulfill the request for safety reasons.

    See [more on refusals](https://platform.openai.com/docs/guides/structured-outputs/refusals).
    N)rC  rD  rE  rF  r   rf   rd   r  r    s    rf   r  c                8   | j                  d      xs d}| j                  d      xs d}| j                  d      xs ||z   }|dvrd }|r| dnd}d| j                  d	      xs i j                  d
      | d| j                  d	      xs i j                  d      i}d| j                  d      xs i j                  d
      | d| j                  d      xs i j                  d      i}|6||j                  | dd      z
  ||<   ||j                  | dd      z
  ||<   t        |||t        di |j                         D 	ci c]  \  }}	|		||	 c}	}t	        di |j                         D 	ci c]  \  }}	|		||	 c}	}      S c c}	}w c c}	}w )Nprompt_tokensr   completion_tokenstotal_tokens>   flexpriorityr  rs   rx   prompt_tokens_detailsaudio_tokens
cache_readcached_tokenscompletion_tokens_detailsr   reasoning_tokensinput_tokensoutput_tokensr  input_token_detailsoutput_token_detailsr   r`   r0   r.   r   r/   )
oai_token_usager  r  r  r  service_tier_prefixr  r  r   r   s
             rd   ra  ra  '  s    #&&7<1L#''(;<AM"&&~6V,:VL//0<\N!,"/%%&=>D"II
 
z* 78>B
#o
! 	/%%&ABHbMM
 
y) ;<B
# 
!"  -9;N;R;R"#:.<
 -
L) .;=Q=U=U"#9-q>
 .
\* !#!- 
 3 9 9 ;M1q}q!tM
 0 
 4 : : <N1q!tN

 

 N Os   
FF5
F Fc                   | j                  dd      }| j                  dd      }| j                  d||z         }|dvrd }|r| dnd}| d| j                  d	      xs i j                  d
      i}| d| j                  d      xs i j                  d      i}|6||j                  | dd      z
  ||<   ||j                  | dd      z
  ||<   t        |||t        di |j                         D 	ci c]  \  }}	|		||	 c}	}t	        di |j                         D 	ci c]  \  }}	|		||	 c}	}      S c c}	}w c c}	}w )Nr  r   r  r  >   r  r  r  rs   r   output_tokens_detailsr  r  input_tokens_detailsr  r  r   r  )
r  r  r  r  r  r  r  r  r   r   s
             rd    _create_usage_metadata_responsesr  V  s    #&&~q9L#'';M"&&~|m7STL//0<\N!,"
y) 78>B
# 
!" 
z* 67=2
#o
!
  .;=Q=U=U"#9-q>
 .
\* -9;N;R;R"#:.<
 -
L) !#!- 
 3 9 9 ;M1q}q!tM
 0 
 4 : : <N1q!tN

 

 N Os   5
D? D?$
E/Ec                    d| v xr | d   dk7  S )Nr   r   r   )r   s    rd   _is_builtin_toolr    s    T>8d6lj88rf   c                |    d| v xr t        d | d   D              }h d}t        |xs |j                  |             S )Nr  c              3  2   K   | ]  }t        |        y wr^   )r  )r,  r   s     rd   r/  z%_use_responses_api.<locals>.<genexpr>  s      4#'4s   >   r{  r  r   r  r  )r   r   intersection)r  uses_builtin_toolsresponses_only_argss      rd   r  r    sR     G+  4+27+;4 1 "O&9&F&Fw&OPPrf   c                    t        t        |       dz
  dd      D ]S  }| |   }t        |t              s|j                  j                  d      }|s7|j                  d      sI| |dz   d |fc S  | dfS )a  Get the last part of the conversation after the last `AIMessage` with an `id`.

    Will return:

    1. Every message after the most-recent `AIMessage` that has a non-empty
        `response_metadata["id"]` (may be an empty list),
    2. That `id`.

    If the most-recent `AIMessage` does not have an `id` (or there is no
    `AIMessage` at all) the entire conversation is returned together with `None`.
    r  rp  rq   resp_N)rangerb  r   r   rc  r`   r   )r}  ir   response_ids       rd   r  r    s     3x=1$b"- 6qkc9%//33D9K{55g>A(+556 T>rf   c                h   dD ]  }||v s|j                  |      |d<    d|v rd|vrd|j                  d      i|d<   |j                  d      xs d}|j                  d      r=d	|vr9|j                  d      xs i j                  d      d
k7  r|j                  dd        t        |       |d<   |j                  dd       x}r|g }|D ]p  }|d   dk(  rd|v r|j	                  ddi|d          (|d   dk(  r0d|v rd}t        |      |j                  d      rd|vr	i |ddi}n	 |j	                  |       r ||d<   |j                  dd       x}r.t        |t              r|d   dk(  rd|v rddi|d   |d<   n||d<   |j                  dd       x}	r|j                  dd       }
|j                  d      st        |	      r|	|d<   nt        |	      r|	j                         }d}
n|	}|ddik(  r,d|v rt        |d   t              rddi|d   d<   ncdddii|d<   nYt        ||
      x}rIt        |t              r9|d   dk(  r1ddi|d   }d|v rt        |d   t              r	||d   d<   n	d|i|d<   n	 |j                  dd       }|(d|v rt        |d   t              r
||d   d<   |S d|i|d<   |S ) N)r   r  max_output_tokensr   r   r!  r   rs   r  r  r   r   inputr  r   r   rm   partial_imageszPartial image generation is not yet supported via the LangChain ChatOpenAI client. Please drop the 'partial_images' key from the image_generation tool.rL  r  r  ro  r  text_formatTr  r{  formatr  r  r   )r"  r`   r   _construct_responses_api_inputr   r  r   r   r  model_json_schemar  )r}  r  legacy_token_paramr   r  	new_toolsr   r   r  rq  r  schema_dictro  format_valuer   s                  rd   r  r    sq    F K(+2;;7I+JG'(K W$G)C ('++6H*IJ
 KK &BE!5 [[%+00:fDM4(5h?GGGT**u*	 	'D F|z)jD.@  &*!IZ8H!IJ<#55'4/$  2#66{{8,1A1M  =$<(8!<  &/	'2 %kk-66{6 {D)F#z1k)&,j%TK
<S%TGM"%0GM" .55v5 Xt,{{8$);F)C%+GM"!&)$668$v}55W$GFOT)J170GGFOH-'/&-1H&IGFO (J#F( O   6$V,= &X9WXW$GFOT)J0<GFOH-'/&>GFOK.IWGFOT!B+4GFOK( N  +I6GFONrf   c                    | j                  d      dk(  r*d| v r&| j                         }|j                  d      |d<   |S | S )Nr   file_citationr   
file_indexr`   ra   r"  
annotationnew_annotations     rd   _format_annotation_to_lcr    sI     ~~f0W
5J#*'5'9'9''B|$rf   c                    | j                  d      dk(  r*d| v r&| j                         }|j                  d      |d<   |S | S )Nr   r  r  r   r  r  s     rd   _format_annotation_from_lcr    sG    ~~f0\Z5O#*"0"4"4\"Bwrf   c                    | d   dk(  rd| d   dS | d   dk(  r,d| d   d   d}| d   j                  d	      r| d   d	   |d	<   |S | d   d
k(  r	ddi| d
   S | S )zConvert chat completions content blocks to Responses API format.

    Only handles text, image, file blocks. Others pass through.
    r   r{  
input_text)r   r{  r   input_imager   r   r  r  
input_file)r`   )r   	new_blocks     rd   -_convert_chat_completions_blocks_to_responsesr  '  s     V} %eFm<<V}# "{+E2
	 !!(+"'"4X">IhV}6f66Lrf   c                    t        | t              r| S t        | t              r+t        d | D              r| D cg c]  }t	        |       c}S t        |       S c c}w )Nc              3  f   K   | ])  }t        |t              xr |j                  d       dv  + yw)r   )r  r  r  r{  r   r  Nr  r  s     rd   r/  z5_ensure_valid_tool_message_content.<locals>.<genexpr>D  sA      -  	5$ 		
IIf

		
-s   /1)r   r   r   r5  r  rC   )tool_outputr   s     rd   "_ensure_valid_tool_message_contentr  A  sf    +s#+t$ - !- * %
 :%@
 	
 k""	
s   Ac                f   d }t        | j                  t              r| j                  D ]  }| j                  j	                  d      dk(  r6t        |t
              r&|j	                  d      dk(  r| j                  d|d} nt        |t
              sh|j	                  d      dk(  s}|j	                  di       j	                  d      dk(  s|d   } n= n;| j                  j	                  d      dk(  r| j                  dd| j                  dd}| d| j                  v r| j                  d   |d<   |S )	Nr   computer_call_outputr  )call_idr   rS  r  r  r   acknowledged_safety_checks)r   r-   r   ry   r`   r   r   )r   r  r   s      rd   '_make_computer_call_output_from_messager  X  s;    37'//4(__ 	E))--f59OOud+IIf%6  '332#($
 5$'IIf%7IIgr*..v6:PP',W~$'	( 
	"	"	&	&v	.2H	H ++*,7??K 
 	((G,E,EE=D=V=V(>
9:  rf   c                d   d }| j                   D ]  }t        |t              r:|j                  d      dk(  r&d| j                  |j                  d      xs dd} |S t        |t              s^|j                  d      dk(  ss|j                  di       j                  d      dk(  s|d   } |S  |S )Nr   custom_tool_call_outputrS  rs   )r   r  rS  r  r  )r-   r   r   r`   r   )r   custom_tool_outputr   s      rd   %_make_custom_tool_output_from_messager    s     eT"uyy'8<U'U1"//))H-3"
   ud#		&!^3		'2&**626OO!&w!  rf   c                4   | j                         D ci c]  \  }}|dk7  s|| }}}d|v r^t        |d   t              rKg }|d   D ]<  }|j                         D ci c]  \  }}|dk7  s|| }}}|j                  |       > ||d<   |S c c}}w c c}}w )zWhen streaming, `langchain-core` uses `index` to aggregate text blocks.

    OpenAI API does not support this key, so we need to remove it.
    r   summary)r   r   r   r   )r   r   r   r  new_summary	sub_blocknew_sub_blocks          rd   _pop_index_and_sub_indexr    s    
 #(++-@$!Q1<A@I@I*Yy-A4"H"9- 	.I.7oo.?Pda1<QTPMP}-	.  +	) A Qs   BBB,Bc           
     	   g }| D ]p  }t        |t              rt        |      }t        |d      }t        |j	                  d      t
              rt        d |d   D              r|j                  D cg c]  }d|d   |d   |j	                  d      d	  }}t        |d   |      |d<   ngt        |d      }t        |d   t
              rGt        |d         D ]6  \  }}t        |t              s|j	                  d
      dk(  s,|d   |d   |<   8 d|v r|j                  d       |d   dk(  rz|d   }t        t        t        |            }	t        |      }
|	r|j!                  |	       h|
r|j!                  |
       }t#        |      }d||d   d}|j!                  |       |d   dk(  rt        |j	                  d      t
              r2|d   D ](  }t        |t              s|j	                  d
      x}s)|dv r|j	                  d      }|dv rE|j	                  d      }|Vd||j	                  d      xs g D cg c]  }t%        |       c}d}n|dk(  rd|d   d}|D ]:  }|j	                  d      x}s||k(  sd|vrg |d<   |d   j!                           |j!                  dgd|d       |dv r|j!                  t'        |             |dk(  r|j!                  d|d   d        )+ n<t        |j	                  d      t(              r|j!                  ddd|d   g dgd!       |j                  d"d      x}sQ|D ch c]  }|j	                  d
      d#v r	d$|v r|d$     }}|D ]1  }|d   |vsd%|d&   d   |d&   d'   |d   d(}|j!                  |       3 |d   d)v rt        |d   t
              rg }d*}|d   D ]V  }|d
   d+v r|j!                  t+        |             %|d
   d,v r|j!                  |       >|d
   |v r|j!                  |       WX ||d<   |d   s:|j!                  |       M|j!                  |       `|j!                  |       s |S c c}w c c}w c c}w )-z1Construct the input for the OpenAI Responses API.r   r   r-   c              3  <   K   | ]  }t        |t                y wr^   r  r  s     rd   r/  z1_construct_responses_api_input.<locals>.<genexpr>  s      <,1
5$'<s   r   rp   r   rq   r   rp   r   rq   r   r  r  ro   r   function_call_outputr   )r   rS  r  rt   )r{  output_textr  )r{  r  r{  Nr  r   )r   r{  r   r  r   r  r   )r   r-   ro   rq   )
r   web_search_callfile_search_callru   computer_callcustom_tool_callr   mcp_callmcp_list_toolsmcp_approval_requestimage_generation_call)r   rq   )r   ro   r-   rv   )ru   r  r  ru   r   r   )r   rp   r   r  )rr   r|   r}   )mcp_approval_response)r{  r   r  )r  r  r  )r   r   r1   r   r`   r   r5  rv   rW   	enumerater   r"  r  r   r*   r  r   r  r  r  r   r  )r}  r  lc_msgr   r   tcsr  r   r  r  r  r  
block_typemsg_idr{  r  r  itemitem_idrv   content_call_idsru   
new_blocksnon_message_item_typess                           rd   r  r    s1   F bfi(1&9F*6{CC#''),d3 <58^< 9 &,%6%6- " !, )& 1 )& 1'mmD1	- - "?s9~s!SI*6{CC#i.$/ )#i. 9 ;HAu!%.599V3D3V,1'NIq); S=GGFOv;& i.K#J[&)$  "Gv!N#23#01@M2)">2($
 23[K'#''),d3 ^ <!E!%.%))FBS4SJ4S%)KK%*YYt_F)-DD',yy'8#'<$,,9,0 ;@))M:R:XVX4&,6 )C:(N4&-"	 ",y!8,5/4Y/?-"	 )/ "/3xx~$=G$=7fCT'0'<:<Y$(O$:$:9$E$)" !'094=;0;.4	%&!" ( ,  #MM*B5*IJ'+BB"MM)@d T !y<!z CGGI.4 ) + )6(+I/1$
 !WW\488z8 "($yy(,QQ!U* )$$  $ ", 5I .>>$3$-j$9&$A)2:)>{)K'0	) m45 [;;#i.$/
)C& ^ 
EV}(EE"))I%P v*UU"))%0v*@@e,
 ",Iy>MM#&c"MM#EbH M{-l4&@$s   3#Q;R #Rc                   t        | d      r| j                  S | j                  D cg c]=  }|j                  dk(  r,|j                  D ]  }|j                  dk(  r|j
                   ? }}}dj                  |      S c c}}w )zrSafe output text extraction.

    Context: OpenAI SDK deleted `response.output_text` momentarily in `1.99.2`.
    r  r   rs   )r  r  rS  r   r-   r{  join)r  rS  r-   textss       rd   _get_output_textr	  P  s    
 x'### oo;;)#~~	 <<=(	 	E  775>s   AA>c                   | j                   rt        | j                         |d}| j                  dd      j                         D ci c]  \  }}|dv r|| }}}|r|j	                  |       d|d<   |j                  d	      |d
<   | j                  r/t        | j                  j                         | j                        }nd}g }g }	g }
i }| j                  D ]O  }|j                  dk(  r|j                  D ]  }|j                  dk(  rd|j                  t        |j                  t              r1|j                  D cg c]  }t!        |j                                c}ng |j"                  d}|j%                  |       t'        |d      r|j(                  |d<   |j                  dk(  s|j%                  d|j*                  |j"                  d        |j                  dk(  r|j%                  |j                  dd             	 t-        j.                  |j0                  d      }d}|.d|j6                  ||j8                  d}|	j%                  |       d|j6                  ||j8                  |d}|
j%                  |       |j                  dk(  r\|j%                  |j                  dd             d|j6                  d|j:                  i|j8                  d}|	j%                  |       |j                  dv s.|j%                  |j                  dd             R t=        |       }|d|vr|r| j                  rw| j                  j                         x}r[|j                  di       x}rG|j                  d      dk(  r3	 t-        j.                  |      }|rt?        |      r	 |d#i |}n|}||d<   tA        || j"                  ||||	|
      }|d k(  rtC        |      }tE        tG        |!      g"      S c c}}w c c}w # t2        $ r"}|j0                  }t5        |      }Y d}~	d}~ww xY w# t,        j2                  $ r Y w xY w)$z;Construct `ChatResponse` from OpenAI Response API response.Nr  Tri  exclude_noner  )	
created_atrq   incomplete_detailsrr  objectstatusrr   r   r  r;  r`  r   rb   r   r  r{  )r   r{  r   rq   r  r  )r   r  rq   ru   Fr  r   r  rl  )r   rp   r   rq   r  r  __arg1)	r   r  r  r  r   r  r  r  r  r  r   r  )r-   rq   rZ  rc  ry   rv   rz   v0r   )r  r   )$r  r   r  r   updater`   rW  r  r  rS  r   r-   r{  r   r   r   r  rq   r   r  r  r  ri  loadsr   r   r   rp   r  r  r	  r  r   rX   r<   r:   )r  rq  rr  r  r   r   rc  rZ  r  rv   rz   ry   rS  r-   r  r   r   r  r   r   r  text_configformat_parsed_dictr  r   s                             rd   r  r  a  sY    ~~(( ( ''T'GMMOAq

 	
1 $   **2&'&7&;&;G&Dl#~~9NN%%')>)>
 NJ // CU;;)#!>> <<=0 & '
 &g&9&94@ /6.A.A( * 5Z5J5J5LM(
  $ii
E #))%0w16=nn)(3<<9,"))!*wfiiX#( [[O+!!&"3"3F"3"STzz&"2"25A }'"KK  ..		 !!), 0"KK  .."	 #)))4[[..!!&"3"3F"3"ST#!6<<0nn	I i([[ 

 

 !!&"3"3F"3"STGCUh #8,K--MM$MM4466[6#"55W5[[ M1	**[1K,V4.+.$*0h' ;;%++-G ,W5>'#B"CDDmR(( # ''AT ## 		s6   
P  P#P2Q  	P=P88P= QQc           	        d4d5fd}|d}g }	g }
i }|xs i }d|d<   d }d }d }| j                   dk(  r> || j                  | j                         |	j                  d| j                  d       n	| j                   dk(  r || j                  | j                         t        | j                  t              r| j                  }n| j                  j                  d	d
      }|	j                  dt        |      gd       nx| j                   dk(  r? || j                  | j                         |	j                  dd| j                  d       n*| j                   dk(  r1| j                  j                  }| j                  j                  |d<   n| j                   dv rt        t        t        | j                  ||      j                   d   j"                        }|j$                  j'                  d      x}r||d<   |j(                  }|j*                  j-                         D ci c]  \  }}|dk7  s|| }}}d}n:| j                   dk(  r8| j.                  j                   dk(  r|dk(  r| j.                  j                  }nn| j                   dk(  r| j.                  j                   dk(  r || j                         |
j                  d| j.                  j0                  | j.                  j2                  | j.                  j4                  d       |	j                  d| j.                  j0                  | j.                  j2                  | j.                  j4                  | j.                  j                  d       n| j                   dk(  r_| j.                  j                   dv rG || j                         | j.                  j                  d	d
      }|d <   |	j                  |       n| j                   dk(  r| j.                  j                   d!k(  r || j                         | j.                  j                  d	d
      }|d <   |	j                  |       |
j                  d| j.                  j0                  t7        j8                  d"| j.                  j:                  i      | j.                  j4                  d       n| j                   d#k(  rR || j                         |
j                  d| j                  d$       |	j                  d| j                  d%       nV| j                   d&k(  r |	j                  d'| j<                  d(       n'| j                   dk(  ra| j.                  j                   d)k(  rH || j                         d| j.                  j                  d	d
      }|d <   |	j                  |       n| j                   d*k(  rB || j                         |	j                  | j>                  d+dd,gd)| j                  d-       nf| j                   d.k(  rnV| j                   d/k(  rA || j                         |	j                  | j>                  d+| j                  d,gd)d0       nd fS tA        |	|
|||||1      }|dk(  rt        t@        tC        ||2            }tE        |3      fS c c}}w )6Nc                J    || k7  rdz  | y| k7  s|k7  rdz  || y)a  Advance indexes tracked during streaming.

        Example: we stream a response item of the form:

        ```python
        {
            "type": "message",  # output_index 0
            "role": "assistant",
            "id": "msg_123",
            "content": [
                {"type": "output_text", "text": "foo"},  # sub_index 0
                {"type": "output_text", "text": "bar"},  # sub_index 1
            ],
        }
        ```

        This is a single item with a shared `output_index` and two sub-indexes, one
        for each content block.

        This will be processed into an `AIMessage` with two text blocks:

        ```python
        AIMessage(
            [
                {"type": "text", "text": "foo", "id": "msg_123"},  # index 0
                {"type": "text", "text": "bar", "id": "msg_123"},  # index 1
            ]
        )
        ```

        This function just identifies updates in output or sub-indexes and increments
        the current index accordingly.
        Nr  r   )
output_idxsub_idxr  r  r  s     rd   _advancez>_convert_responses_chunk_to_generation_chunk.<locals>._advance  sM    F ?#z1"
  * %
28IW8T" ')rf   r  r;  r`  zresponse.output_text.deltar{  )r   r{  r   z%response.output_text.annotation.addedTri  r  )r   r   r   zresponse.output_text.doners   )r   r{  rq   r   zresponse.createdrq   )zresponse.completedzresponse.incomplete)rq  r  r   r  lastzresponse.output_item.addedr   r  ru   r3   )r   rp   r   rq   r   )r   rp   r   r  rq   r   zresponse.output_item.done)r  r  r  r   r  r  r  r  r   r  r  z&response.function_call_arguments.delta)r   r   r   )r   r   r   zresponse.refusal.doner  r  r   z%response.reasoning_summary_part.addedsummary_text)r   r   r{  )r  r   r   rq   z,response.image_generation_call.partial_imagez%response.reasoning_summary_text.delta)r  r   r   )r-   r   rZ  rc  ry   rq   chunk_position)rs  r  r^   )r  r   r  r   r5  r8  )#r   output_indexcontent_indexr   r^  r   r  r   r  r  r  r  rq   r   r   r  r  r   ry   r`   rZ  rc  r   r  rp   r   r  ri  rj  r  r  summary_indexr   rX   r;   )rY  r  r  r  rq  rr  rs  r  r  r-   r   ry   rc  rZ  r   rq   r  r   r  r   r   r  r   r   s    ```                    rd   ry  ry  
  s,   ** **X  (G  B*2&'N-1N	Bzz11##U%8%89mTU	>	>##U%8%89e&&-))J))44$V4TJ 8 DE&	
 
2	2##U%8%89mm&		
 
)	)^^"'.."3"3$	D	D7NN6. Q  	
 **..x8868*0h'++ 2288:
Qa4iAqD
 
  	3	3

98TT!B

22JJOO.##$)



,,jj((&	
 	'

"ZZ11 ::--jjmm&		
 
2	2uzz 	K 	8 	##$jj++F+K,G{#

11JJOO11##$jj++F+K,G{#)



Hejj.>.>#?@jj((&	
 
?	?##$&mT	
 	$5;;W	
 
.	.	emmDE	3	3

;8V##$JJ))t&)I	*	'y!	>	>##$ $11>SUV '#mm
	
 
E	E	>	>##$ "'!4!4 . % '#
	
 24EtKK)%++%G &wmL
 	G,	 C
s   *[ 8[ )rb   r   r5  rZ   )r   Mapping[str, Any]r5  r   )r   N)r-   r   r   (Literal['chat/completions', 'responses']ro   r   r5  r   )r   )r   r   r   r%  r5  r   )r   r$  r   ztype[BaseMessageChunk]r5  r   )r   
int | dictr   r&  r5  r&  )r   zopenai.BadRequestErrorr5  r8  )rb   r   r5  r   )rd  r   r5  r   )r   r)   r5  r   )rl  r&   r5  r   )rz  r   r5  ztuple[int, int] | None)r{  r   r|  r   r5  r   )r  r   r5  r   )r{  r   r|  r   r5  ztuple[int, int])rq  zdict[str, Any] | typer  r   r5  zdict | TypeBaseModel)r  r   rq  z	type[_BM]r5  zPydanticBaseModel | Noner^   )r  r   r  r   r5  r0   )r   r   r5  r   r>  )r}  r@  r5  z(tuple[Sequence[BaseMessage], str | None])r}  r@  r  r   r5  r   )r  r   r5  r   )r   r   r5  r   )r  r   r5  zstr | list[dict])r   r*   r5  r   )r   r*   r5  r7  )r   r   r5  r   )r}  r@  r5  r   )r  r[   r5  r   )NNN)
r  r[   rq  type[_BM] | Nonerr  r7  r  r   r5  r<   )NNFN)rY  r   r  r   r  r   r  r   rq  r'  rr  r7  rs  r   r  r   r5  z0tuple[int, int, int, ChatGenerationChunk | None])rF  
__future__r   r   ri  loggingr3  r]  sslr  r   collections.abcr   r   r   r   r   r	   	functoolsr
   ior   r   mathr   operatorr   typingr   r   r   r   r   r   urllib.parser   certifir;  r  langchain_core.callbacksr   r   langchain_core.language_modelsr   r   *langchain_core.language_models.chat_modelsr   r   langchain_core.messagesr   r   r   r   r    r!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   typeslangchain_core.messages.air.   r/   r0   0langchain_core.messages.block_translators.openair1   r2   langchain_core.messages.toolr3   langchain_core.output_parsersr4   r5   *langchain_core.output_parsers.openai_toolsr6   r7   r8   r9   langchain_core.outputsr:   r;   r<   langchain_core.runnablesr=   r>   r?   r@   langchain_core.runnables.configrA   langchain_core.toolsrB   langchain_core.tools.baserC   langchain_core.utilsrD   %langchain_core.utils.function_callingrE   rF   langchain_core.utils.pydanticrG   rH   rI   langchain_core.utils.utilsrJ   rK   rL   pydanticrM   rN   rO   rP   rQ   pydantic.v1r  typing_extensionsrR   *langchain_openai.chat_models._client_utilsrS   rT   rU   $langchain_openai.chat_models._compatrV   rW   rX   langchain_openai.data._profilesrY   rZ   openai.types.responsesr[   	getLoggerrC  rq  create_default_contextwherer:  r_   re   r  r   r   r   r   r   r   r   r   r   r   r   r   rG  r   r   rN  r  r   r   r  r  rs  rw  r  r  r!  r   r  ra  r  r  r  r  r  r  r  r  r  r  r  r  r  r	  r  ry  r   rf   rd   <module>rP     s{    "    	 	 
 
          "       & 5 
 9 P  S R  < ) 0 9 
 V U  1 " 
 
 6;/			8	$ 0S//}w}}G +Y7
@LJ 5G::	1: : 		:~ 5GMM	1M 
M`5252-C5252p#0:8
2' e9%"&sCx.49"<t"C i C!CZ '{
] {
|5I
 I
X&@
&
	
<&* =A%!%.9%%P(@  7;,,)3,,` 7;&&)3&&R9Q#-2h#h.2h	hV4#.' ' ' T*gT&  $ !%	fEfEfE fE 	fE
 fE\  $ !%www w 	w
 w w w w 6wrf   