"""OpenAI chat wrapper."""from__future__importannotationsimportbase64importjsonimportloggingimportosimportreimportsslimportsysimportwarningsfromfunctoolsimportpartialfromioimportBytesIOfromjsonimportJSONDecodeErrorfrommathimportceilfromoperatorimportitemgetterfromtypingimport(TYPE_CHECKING,Any,AsyncIterator,Callable,Dict,Iterator,List,Literal,Mapping,Optional,Sequence,Tuple,Type,TypedDict,TypeVar,Union,cast,)fromurllib.parseimporturlparseimportcertifiimportopenaiimporttiktokenfromlangchain_core._api.deprecationimportdeprecatedfromlangchain_core.callbacksimport(AsyncCallbackManagerForLLMRun,CallbackManagerForLLMRun,)fromlangchain_core.language_modelsimportLanguageModelInputfromlangchain_core.language_models.chat_modelsimport(BaseChatModel,LangSmithParams,agenerate_from_stream,generate_from_stream,)fromlangchain_core.messagesimport(AIMessage,AIMessageChunk,BaseMessage,BaseMessageChunk,ChatMessage,ChatMessageChunk,FunctionMessage,FunctionMessageChunk,HumanMessage,HumanMessageChunk,InvalidToolCall,SystemMessage,SystemMessageChunk,ToolCall,ToolMessage,ToolMessageChunk,)fromlangchain_core.messages.aiimport(InputTokenDetails,OutputTokenDetails,UsageMetadata,)fromlangchain_core.messages.toolimporttool_call_chunkfromlangchain_core.output_parsersimportJsonOutputParser,PydanticOutputParserfromlangchain_core.output_parsers.openai_toolsimport(JsonOutputKeyToolsParser,PydanticToolsParser,make_invalid_tool_call,parse_tool_call,)fromlangchain_core.outputsimportChatGeneration,ChatGenerationChunk,ChatResultfromlangchain_core.runnablesimport(Runnable,RunnableLambda,RunnableMap,RunnablePassthrough,)fromlangchain_core.runnables.configimportrun_in_executorfromlangchain_core.toolsimportBaseToolfromlangchain_core.tools.baseimport_stringifyfromlangchain_core.utilsimportget_pydantic_field_namesfromlangchain_core.utils.function_callingimport(convert_to_openai_function,convert_to_openai_tool,)fromlangchain_core.utils.pydanticimport(PydanticBaseModel,TypeBaseModel,is_basemodel_subclass,)fromlangchain_core.utils.utilsimport_build_model_kwargs,from_env,secret_from_envfrompydanticimportBaseModel,ConfigDict,Field,SecretStr,model_validatorfrompydantic.v1importBaseModelasBaseModelV1fromtyping_extensionsimportSelfifTYPE_CHECKING:fromopenai.types.responsesimportResponselogger=logging.getLogger(__name__)# This SSL context is equivelent to the default `verify=True`.# https://www.python-httpx.org/advanced/ssl/#configuring-client-instancesglobal_ssl_context=ssl.create_default_context(cafile=certifi.where())_FUNCTION_CALL_IDS_MAP_KEY="__openai_function_call_ids__"def_convert_dict_to_message(_dict:Mapping[str,Any])->BaseMessage:"""Convert a dictionary to a LangChain message. Args: _dict: The dictionary. Returns: The LangChain message. """role=_dict.get("role")name=_dict.get("name")id_=_dict.get("id")ifrole=="user":returnHumanMessage(content=_dict.get("content",""),id=id_,name=name)elifrole=="assistant":# Fix for azure# Also OpenAI returns None for tool invocationscontent=_dict.get("content","")or""additional_kwargs:Dict={}iffunction_call:=_dict.get("function_call"):additional_kwargs["function_call"]=dict(function_call)tool_calls=[]invalid_tool_calls=[]ifraw_tool_calls:=_dict.get("tool_calls"):additional_kwargs["tool_calls"]=raw_tool_callsforraw_tool_callinraw_tool_calls:try:tool_calls.append(parse_tool_call(raw_tool_call,return_id=True))exceptExceptionase:invalid_tool_calls.append(make_invalid_tool_call(raw_tool_call,str(e)))ifaudio:=_dict.get("audio"):additional_kwargs["audio"]=audioreturnAIMessage(content=content,additional_kwargs=additional_kwargs,name=name,id=id_,tool_calls=tool_calls,invalid_tool_calls=invalid_tool_calls,)elifrolein("system","developer"):ifrole=="developer":additional_kwargs={"__openai_role__":role}else:additional_kwargs={}returnSystemMessage(content=_dict.get("content",""),name=name,id=id_,additional_kwargs=additional_kwargs,)elifrole=="function":returnFunctionMessage(content=_dict.get("content",""),name=cast(str,_dict.get("name")),id=id_)elifrole=="tool":additional_kwargs={}if"name"in_dict:additional_kwargs["name"]=_dict["name"]returnToolMessage(content=_dict.get("content",""),tool_call_id=cast(str,_dict.get("tool_call_id")),additional_kwargs=additional_kwargs,name=name,id=id_,)else:returnChatMessage(content=_dict.get("content",""),role=role,id=id_)# type: ignore[arg-type]def_format_message_content(content:Any)->Any:"""Format message content."""ifcontentandisinstance(content,list):formatted_content=[]forblockincontent:# Remove unexpected block typesif(isinstance(block,dict)and"type"inblockandblock["type"]in("tool_use","thinking")):continue# Anthropic image blockselif(isinstance(block,dict)andblock.get("type")=="image"and(source:=block.get("source"))andisinstance(source,dict)):ifsource.get("type")=="base64"and((media_type:=source.get("media_type"))and(data:=source.get("data"))):formatted_content.append({"type":"image_url","image_url":{"url":f"data:{media_type};base64,{data}"},})elifsource.get("type")=="url"and(url:=source.get("url")):formatted_content.append({"type":"image_url","image_url":{"url":url}})else:continueelse:formatted_content.append(block)else:formatted_content=contentreturnformatted_contentdef_convert_message_to_dict(message:BaseMessage)->dict:"""Convert a LangChain message to a dictionary. Args: message: The LangChain message. Returns: The dictionary. """message_dict:Dict[str,Any]={"content":_format_message_content(message.content)}if(name:=message.nameormessage.additional_kwargs.get("name"))isnotNone:message_dict["name"]=name# populate role and additional message dataifisinstance(message,ChatMessage):message_dict["role"]=message.roleelifisinstance(message,HumanMessage):message_dict["role"]="user"elifisinstance(message,AIMessage):message_dict["role"]="assistant"if"function_call"inmessage.additional_kwargs:message_dict["function_call"]=message.additional_kwargs["function_call"]ifmessage.tool_callsormessage.invalid_tool_calls:message_dict["tool_calls"]=[_lc_tool_call_to_openai_tool_call(tc)fortcinmessage.tool_calls]+[_lc_invalid_tool_call_to_openai_tool_call(tc)fortcinmessage.invalid_tool_calls]elif"tool_calls"inmessage.additional_kwargs:message_dict["tool_calls"]=message.additional_kwargs["tool_calls"]tool_call_supported_props={"id","type","function"}message_dict["tool_calls"]=[{k:vfork,vintool_call.items()ifkintool_call_supported_props}fortool_callinmessage_dict["tool_calls"]]else:pass# If tool calls present, content null value should be None not empty string.if"function_call"inmessage_dictor"tool_calls"inmessage_dict:message_dict["content"]=message_dict["content"]orNoneif"audio"inmessage.additional_kwargs:# openai doesn't support passing the data back - only the id# https://platform.openai.com/docs/guides/audio/multi-turn-conversationsraw_audio=message.additional_kwargs["audio"]audio=({"id":message.additional_kwargs["audio"]["id"]}if"id"inraw_audioelseraw_audio)message_dict["audio"]=audioelifisinstance(message,SystemMessage):message_dict["role"]=message.additional_kwargs.get("__openai_role__","system")elifisinstance(message,FunctionMessage):message_dict["role"]="function"elifisinstance(message,ToolMessage):message_dict["role"]="tool"message_dict["tool_call_id"]=message.tool_call_idsupported_props={"content","role","tool_call_id"}message_dict={k:vfork,vinmessage_dict.items()ifkinsupported_props}else:raiseTypeError(f"Got unknown type {message}")returnmessage_dictdef_convert_delta_to_message_chunk(_dict:Mapping[str,Any],default_class:Type[BaseMessageChunk])->BaseMessageChunk:id_=_dict.get("id")role=cast(str,_dict.get("role"))content=cast(str,_dict.get("content")or"")additional_kwargs:Dict={}if_dict.get("function_call"):function_call=dict(_dict["function_call"])if"name"infunction_callandfunction_call["name"]isNone:function_call["name"]=""additional_kwargs["function_call"]=function_calltool_call_chunks=[]ifraw_tool_calls:=_dict.get("tool_calls"):additional_kwargs["tool_calls"]=raw_tool_callstry:tool_call_chunks=[tool_call_chunk(name=rtc["function"].get("name"),args=rtc["function"].get("arguments"),id=rtc.get("id"),index=rtc["index"],)forrtcinraw_tool_calls]exceptKeyError:passifrole=="user"ordefault_class==HumanMessageChunk:returnHumanMessageChunk(content=content,id=id_)elifrole=="assistant"ordefault_class==AIMessageChunk:returnAIMessageChunk(content=content,additional_kwargs=additional_kwargs,id=id_,tool_call_chunks=tool_call_chunks,# type: ignore[arg-type])elifrolein("system","developer")ordefault_class==SystemMessageChunk:ifrole=="developer":additional_kwargs={"__openai_role__":"developer"}else:additional_kwargs={}returnSystemMessageChunk(content=content,id=id_,additional_kwargs=additional_kwargs)elifrole=="function"ordefault_class==FunctionMessageChunk:returnFunctionMessageChunk(content=content,name=_dict["name"],id=id_)elifrole=="tool"ordefault_class==ToolMessageChunk:returnToolMessageChunk(content=content,tool_call_id=_dict["tool_call_id"],id=id_)elifroleordefault_class==ChatMessageChunk:returnChatMessageChunk(content=content,role=role,id=id_)else:returndefault_class(content=content,id=id_)# type: ignoredef_update_token_usage(overall_token_usage:Union[int,dict],new_usage:Union[int,dict])->Union[int,dict]:# Token usage is either ints or dictionaries# `reasoning_tokens` is nested inside `completion_tokens_details`ifisinstance(new_usage,int):ifnotisinstance(overall_token_usage,int):raiseValueError(f"Got different types for token usage: "f"{type(new_usage)} and {type(overall_token_usage)}")returnnew_usage+overall_token_usageelifisinstance(new_usage,dict):ifnotisinstance(overall_token_usage,dict):raiseValueError(f"Got different types for token usage: "f"{type(new_usage)} and {type(overall_token_usage)}")return{k:_update_token_usage(overall_token_usage.get(k,0),v)fork,vinnew_usage.items()}else:warnings.warn(f"Unexpected type for token usage: {type(new_usage)}")returnnew_usagedef_handle_openai_bad_request(e:openai.BadRequestError)->None:if("'response_format' of type 'json_schema' is not supported with this model")ine.message:message=("This 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"`.')warnings.warn(message)raiseeelif"Invalid schema for response_format"ine.message:message=("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"# noqa: E501)warnings.warn(message)raiseeelse:raiseclass_FunctionCall(TypedDict):name:str_BM=TypeVar("_BM",bound=BaseModel)_DictOrPydanticClass=Union[Dict[str,Any],Type[_BM],Type]_DictOrPydantic=Union[Dict,_BM]class_AllReturnType(TypedDict):raw:BaseMessageparsed:Optional[_DictOrPydantic]parsing_error:Optional[BaseException]
[docs]classBaseChatOpenAI(BaseChatModel):client:Any=Field(default=None,exclude=True)#: :meta private:async_client:Any=Field(default=None,exclude=True)#: :meta private:root_client:Any=Field(default=None,exclude=True)#: :meta private:root_async_client:Any=Field(default=None,exclude=True)#: :meta private:model_name:str=Field(default="gpt-3.5-turbo",alias="model")"""Model name to use."""temperature:Optional[float]=None"""What sampling temperature to use."""model_kwargs:Dict[str,Any]=Field(default_factory=dict)"""Holds any model parameters valid for `create` call not explicitly specified."""openai_api_key:Optional[SecretStr]=Field(alias="api_key",default_factory=secret_from_env("OPENAI_API_KEY",default=None))openai_api_base:Optional[str]=Field(default=None,alias="base_url")"""Base URL path for API requests, leave blank if not using a proxy or service emulator."""openai_organization:Optional[str]=Field(default=None,alias="organization")"""Automatically inferred from env var `OPENAI_ORG_ID` if not provided."""# to support explicit proxy for OpenAIopenai_proxy:Optional[str]=Field(default_factory=from_env("OPENAI_PROXY",default=None))request_timeout:Union[float,Tuple[float,float],Any,None]=Field(default=None,alias="timeout")"""Timeout for requests to OpenAI completion API. Can be float, httpx.Timeout or None."""max_retries:Optional[int]=None"""Maximum number of retries to make when generating."""presence_penalty:Optional[float]=None"""Penalizes repeated tokens."""frequency_penalty:Optional[float]=None"""Penalizes repeated tokens according to frequency."""seed:Optional[int]=None"""Seed for generation"""logprobs:Optional[bool]=None"""Whether to return logprobs."""top_logprobs:Optional[int]=None"""Number of most likely tokens to return at each token position, each with an associated log probability. `logprobs` must be set to true if this parameter is used."""logit_bias:Optional[Dict[int,int]]=None"""Modify the likelihood of specified tokens appearing in the completion."""streaming:bool=False"""Whether to stream the results or not."""n:Optional[int]=None"""Number of chat completions to generate for each prompt."""top_p:Optional[float]=None"""Total probability mass of tokens to consider at each step."""max_tokens:Optional[int]=Field(default=None)"""Maximum number of tokens to generate."""reasoning_effort:Optional[str]=None"""Constrains effort on reasoning for reasoning models. Reasoning models only, like OpenAI o1 and o3-mini. Currently supported values are low, medium, and high. Reducing reasoning effort can result in faster responses and fewer tokens used on reasoning in a response. .. versionadded:: 0.2.14 """tiktoken_model_name:Optional[str]=None"""The model name to pass to tiktoken when using this class. Tiktoken is used to count the number of tokens in documents to constrain them to be under a certain limit. By default, when set to None, this will be the same as the embedding model name. However, there are some cases where you may want to use this Embedding class with a model name not supported by tiktoken. This can include when using Azure embeddings or when using one of the many model providers that expose an OpenAI-like API but with different models. In those cases, in order to avoid erroring when tiktoken is called, you can specify a model name to use here."""default_headers:Union[Mapping[str,str],None]=Nonedefault_query:Union[Mapping[str,object],None]=None# Configure a custom httpx client. See the# [httpx documentation](https://www.python-httpx.org/api/#client) for more details.http_client:Union[Any,None]=Field(default=None,exclude=True)"""Optional httpx.Client. Only used for sync invocations. Must specify http_async_client as well if you'd like a custom client for async invocations. """http_async_client:Union[Any,None]=Field(default=None,exclude=True)"""Optional httpx.AsyncClient. Only used for async invocations. Must specify http_client as well if you'd like a custom client for sync invocations."""stop:Optional[Union[List[str],str]]=Field(default=None,alias="stop_sequences")"""Default stop sequences."""extra_body:Optional[Mapping[str,Any]]=None"""Optional additional JSON properties to include in the request parameters when making requests to OpenAI compatible APIs, such as vLLM."""include_response_headers:bool=False"""Whether to include response headers in the output message response_metadata."""disabled_params:Optional[Dict[str,Any]]=Field(default=None)"""Parameters of the OpenAI client or chat.completions endpoint that should be disabled for the given model. Should be specified as ``{"param": None | ['val1', 'val2']}`` where the key is the parameter and the value is either None, meaning that parameter should never be used, or it's a list of disabled values for the parameter. For example, older models may not support the 'parallel_tool_calls' parameter at all, in which case ``disabled_params={"parallel_tool_calls": None}`` can be passed in. If a parameter is disabled then it will not be used by default in any methods, e.g. in :meth:`~langchain_openai.chat_models.base.ChatOpenAI.with_structured_output`. However this does not prevent a user from directly passed in the parameter during invocation. """use_responses_api:Optional[bool]=None"""Whether to use the Responses API instead of the Chat API. If not specified then will be inferred based on invocation params. .. versionadded:: 0.3.9 """model_config=ConfigDict(populate_by_name=True)@model_validator(mode="before")@classmethoddefbuild_extra(cls,values:Dict[str,Any])->Any:"""Build extra kwargs from additional params that were passed in."""all_required_field_names=get_pydantic_field_names(cls)values=_build_model_kwargs(values,all_required_field_names)returnvalues@model_validator(mode="before")@classmethoddefvalidate_temperature(cls,values:Dict[str,Any])->Any:"""Currently o1 models only allow temperature=1."""model=values.get("model_name")orvalues.get("model")or""ifmodel.startswith("o1")and"temperature"notinvalues:values["temperature"]=1returnvalues@model_validator(mode="after")defvalidate_environment(self)->Self:"""Validate that api key and python package exists in environment."""ifself.nisnotNoneandself.n<1:raiseValueError("n must be at least 1.")elifself.nisnotNoneandself.n>1andself.streaming:raiseValueError("n must be 1 when streaming.")# Check OPENAI_ORGANIZATION for backwards compatibility.self.openai_organization=(self.openai_organizationoros.getenv("OPENAI_ORG_ID")oros.getenv("OPENAI_ORGANIZATION"))self.openai_api_base=self.openai_api_baseoros.getenv("OPENAI_API_BASE")client_params:dict={"api_key":(self.openai_api_key.get_secret_value()ifself.openai_api_keyelseNone),"organization":self.openai_organization,"base_url":self.openai_api_base,"timeout":self.request_timeout,"default_headers":self.default_headers,"default_query":self.default_query,}ifself.max_retriesisnotNone:client_params["max_retries"]=self.max_retriesifself.openai_proxyand(self.http_clientorself.http_async_client):openai_proxy=self.openai_proxyhttp_client=self.http_clienthttp_async_client=self.http_async_clientraiseValueError("Cannot specify 'openai_proxy' if one of ""'http_client'/'http_async_client' is already specified. Received:\n"f"{openai_proxy=}\n{http_client=}\n{http_async_client=}")ifnotself.client:ifself.openai_proxyandnotself.http_client:try:importhttpxexceptImportErrorase:raiseImportError("Could not import httpx python package. ""Please install it with `pip install httpx`.")fromeself.http_client=httpx.Client(proxy=self.openai_proxy,verify=global_ssl_context)sync_specific={"http_client":self.http_client}self.root_client=openai.OpenAI(**client_params,**sync_specific)# type: ignore[arg-type]self.client=self.root_client.chat.completionsifnotself.async_client:ifself.openai_proxyandnotself.http_async_client:try:importhttpxexceptImportErrorase:raiseImportError("Could not import httpx python package. ""Please install it with `pip install httpx`.")fromeself.http_async_client=httpx.AsyncClient(proxy=self.openai_proxy,verify=global_ssl_context)async_specific={"http_client":self.http_async_client}self.root_async_client=openai.AsyncOpenAI(**client_params,**async_specific,# type: ignore[arg-type])self.async_client=self.root_async_client.chat.completionsreturnself@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for calling OpenAI API."""exclude_if_none={"presence_penalty":self.presence_penalty,"frequency_penalty":self.frequency_penalty,"seed":self.seed,"top_p":self.top_p,"logprobs":self.logprobs,"top_logprobs":self.top_logprobs,"logit_bias":self.logit_bias,"stop":self.stoporNone,# also exclude empty list for this"max_tokens":self.max_tokens,"extra_body":self.extra_body,"n":self.n,"temperature":self.temperature,"reasoning_effort":self.reasoning_effort,}params={"model":self.model_name,"stream":self.streaming,**{k:vfork,vinexclude_if_none.items()ifvisnotNone},**self.model_kwargs,}returnparamsdef_combine_llm_outputs(self,llm_outputs:List[Optional[dict]])->dict:overall_token_usage:dict={}system_fingerprint=Noneforoutputinllm_outputs:ifoutputisNone:# Happens in streamingcontinuetoken_usage=output.get("token_usage")iftoken_usageisnotNone:fork,vintoken_usage.items():ifvisNone:continueifkinoverall_token_usage:overall_token_usage[k]=_update_token_usage(overall_token_usage[k],v)else:overall_token_usage[k]=vifsystem_fingerprintisNone:system_fingerprint=output.get("system_fingerprint")combined={"token_usage":overall_token_usage,"model_name":self.model_name}ifsystem_fingerprint:combined["system_fingerprint"]=system_fingerprintreturncombineddef_convert_chunk_to_generation_chunk(self,chunk:dict,default_chunk_class:Type,base_generation_info:Optional[Dict],)->Optional[ChatGenerationChunk]:ifchunk.get("type")=="content.delta":# from beta.chat.completions.streamreturnNonetoken_usage=chunk.get("usage")choices=(chunk.get("choices",[])# from beta.chat.completions.streamorchunk.get("chunk",{}).get("choices",[]))usage_metadata:Optional[UsageMetadata]=(_create_usage_metadata(token_usage)iftoken_usageelseNone)iflen(choices)==0:# logprobs is implicitly Nonegeneration_chunk=ChatGenerationChunk(message=default_chunk_class(content="",usage_metadata=usage_metadata))returngeneration_chunkchoice=choices[0]ifchoice["delta"]isNone:returnNonemessage_chunk=_convert_delta_to_message_chunk(choice["delta"],default_chunk_class)generation_info={**base_generation_info}ifbase_generation_infoelse{}iffinish_reason:=choice.get("finish_reason"):generation_info["finish_reason"]=finish_reasonifmodel_name:=chunk.get("model"):generation_info["model_name"]=model_nameifsystem_fingerprint:=chunk.get("system_fingerprint"):generation_info["system_fingerprint"]=system_fingerprintlogprobs=choice.get("logprobs")iflogprobs:generation_info["logprobs"]=logprobsifusage_metadataandisinstance(message_chunk,AIMessageChunk):message_chunk.usage_metadata=usage_metadatageneration_chunk=ChatGenerationChunk(message=message_chunk,generation_info=generation_infoorNone)returngeneration_chunkdef_stream_responses(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[ChatGenerationChunk]:kwargs["stream"]=Truepayload=self._get_request_payload(messages,stop=stop,**kwargs)ifself.include_response_headers:raw_context_manager=self.root_client.with_raw_response.responses.create(**payload)context_manager=raw_context_manager.parse()headers={"headers":dict(raw_context_manager.headers)}else:context_manager=self.root_client.responses.create(**payload)headers={}original_schema_obj=kwargs.get("response_format")withcontext_managerasresponse:is_first_chunk=Trueforchunkinresponse:metadata=headersifis_first_chunkelse{}ifgeneration_chunk:=_convert_responses_chunk_to_generation_chunk(chunk,schema=original_schema_obj,metadata=metadata):ifrun_manager:run_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk)is_first_chunk=Falseyieldgeneration_chunkasyncdef_astream_responses(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->AsyncIterator[ChatGenerationChunk]:kwargs["stream"]=Truepayload=self._get_request_payload(messages,stop=stop,**kwargs)ifself.include_response_headers:raw_context_manager=(awaitself.root_async_client.with_raw_response.responses.create(**payload))context_manager=raw_context_manager.parse()headers={"headers":dict(raw_context_manager.headers)}else:context_manager=awaitself.root_async_client.responses.create(**payload)headers={}original_schema_obj=kwargs.get("response_format")asyncwithcontext_managerasresponse:is_first_chunk=Trueasyncforchunkinresponse:metadata=headersifis_first_chunkelse{}ifgeneration_chunk:=_convert_responses_chunk_to_generation_chunk(chunk,schema=original_schema_obj,metadata=metadata):ifrun_manager:awaitrun_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk)is_first_chunk=Falseyieldgeneration_chunkdef_stream(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->Iterator[ChatGenerationChunk]:kwargs["stream"]=Truepayload=self._get_request_payload(messages,stop=stop,**kwargs)default_chunk_class:Type[BaseMessageChunk]=AIMessageChunkbase_generation_info={}if"response_format"inpayload:ifself.include_response_headers:warnings.warn("Cannot currently include response headers when response_format is ""specified.")payload.pop("stream")response_stream=self.root_client.beta.chat.completions.stream(**payload)context_manager=response_streamelse:ifself.include_response_headers:raw_response=self.client.with_raw_response.create(**payload)response=raw_response.parse()base_generation_info={"headers":dict(raw_response.headers)}else:response=self.client.create(**payload)context_manager=responsetry:withcontext_managerasresponse:is_first_chunk=Trueforchunkinresponse:ifnotisinstance(chunk,dict):chunk=chunk.model_dump()generation_chunk=self._convert_chunk_to_generation_chunk(chunk,default_chunk_class,base_generation_infoifis_first_chunkelse{},)ifgeneration_chunkisNone:continuedefault_chunk_class=generation_chunk.message.__class__logprobs=(generation_chunk.generation_infoor{}).get("logprobs")ifrun_manager:run_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk,logprobs=logprobs,)is_first_chunk=Falseyieldgeneration_chunkexceptopenai.BadRequestErrorase:_handle_openai_bad_request(e)ifhasattr(response,"get_final_completion")and"response_format"inpayload:final_completion=response.get_final_completion()generation_chunk=self._get_generation_chunk_from_completion(final_completion)ifrun_manager:run_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk)yieldgeneration_chunkdef_generate(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[CallbackManagerForLLMRun]=None,**kwargs:Any,)->ChatResult:ifself.streaming:stream_iter=self._stream(messages,stop=stop,run_manager=run_manager,**kwargs)returngenerate_from_stream(stream_iter)payload=self._get_request_payload(messages,stop=stop,**kwargs)generation_info=Noneif"response_format"inpayload:ifself.include_response_headers:warnings.warn("Cannot currently include response headers when response_format is ""specified.")payload.pop("stream")try:response=self.root_client.beta.chat.completions.parse(**payload)exceptopenai.BadRequestErrorase:_handle_openai_bad_request(e)elifself._use_responses_api(payload):original_schema_obj=kwargs.get("response_format")iforiginal_schema_objand_is_pydantic_class(original_schema_obj):response=self.root_client.responses.parse(**payload)else:ifself.include_response_headers:raw_response=self.root_client.with_raw_response.responses.create(**payload)response=raw_response.parse()generation_info={"headers":dict(raw_response.headers)}else:response=self.root_client.responses.create(**payload)return_construct_lc_result_from_responses_api(response,schema=original_schema_obj,metadata=generation_info)elifself.include_response_headers:raw_response=self.client.with_raw_response.create(**payload)response=raw_response.parse()generation_info={"headers":dict(raw_response.headers)}else:response=self.client.create(**payload)returnself._create_chat_result(response,generation_info)def_use_responses_api(self,payload:dict)->bool:ifisinstance(self.use_responses_api,bool):returnself.use_responses_apielse:return_use_responses_api(payload)def_get_request_payload(self,input_:LanguageModelInput,*,stop:Optional[List[str]]=None,**kwargs:Any,)->dict:messages=self._convert_input(input_).to_messages()ifstopisnotNone:kwargs["stop"]=stoppayload={**self._default_params,**kwargs}ifself._use_responses_api(payload):payload=_construct_responses_api_payload(messages,payload)else:payload["messages"]=[_convert_message_to_dict(m)forminmessages]returnpayloaddef_create_chat_result(self,response:Union[dict,openai.BaseModel],generation_info:Optional[Dict]=None,)->ChatResult:generations=[]response_dict=(responseifisinstance(response,dict)elseresponse.model_dump())# Sometimes the AI Model calling will get error, we should raise it.# Otherwise, the next code 'choices.extend(response["choices"])'# will throw a "TypeError: 'NoneType' object is not iterable" error# to mask the true error. Because 'response["choices"]' is None.ifresponse_dict.get("error"):raiseValueError(response_dict.get("error"))token_usage=response_dict.get("usage")forresinresponse_dict["choices"]:message=_convert_dict_to_message(res["message"])iftoken_usageandisinstance(message,AIMessage):message.usage_metadata=_create_usage_metadata(token_usage)generation_info=generation_infoor{}generation_info["finish_reason"]=(res.get("finish_reason")ifres.get("finish_reason")isnotNoneelsegeneration_info.get("finish_reason"))if"logprobs"inres:generation_info["logprobs"]=res["logprobs"]gen=ChatGeneration(message=message,generation_info=generation_info)generations.append(gen)llm_output={"token_usage":token_usage,"model_name":response_dict.get("model",self.model_name),"system_fingerprint":response_dict.get("system_fingerprint",""),}if"id"inresponse_dict:llm_output["id"]=response_dict["id"]ifisinstance(response,openai.BaseModel)andgetattr(response,"choices",None):message=response.choices[0].message# type: ignore[attr-defined]ifhasattr(message,"parsed"):generations[0].message.additional_kwargs["parsed"]=message.parsedifhasattr(message,"refusal"):generations[0].message.additional_kwargs["refusal"]=message.refusalreturnChatResult(generations=generations,llm_output=llm_output)asyncdef_astream(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->AsyncIterator[ChatGenerationChunk]:kwargs["stream"]=Truepayload=self._get_request_payload(messages,stop=stop,**kwargs)default_chunk_class:Type[BaseMessageChunk]=AIMessageChunkbase_generation_info={}if"response_format"inpayload:ifself.include_response_headers:warnings.warn("Cannot currently include response headers when response_format is ""specified.")payload.pop("stream")response_stream=self.root_async_client.beta.chat.completions.stream(**payload)context_manager=response_streamelse:ifself.include_response_headers:raw_response=awaitself.async_client.with_raw_response.create(**payload)response=raw_response.parse()base_generation_info={"headers":dict(raw_response.headers)}else:response=awaitself.async_client.create(**payload)context_manager=responsetry:asyncwithcontext_managerasresponse:is_first_chunk=Trueasyncforchunkinresponse:ifnotisinstance(chunk,dict):chunk=chunk.model_dump()generation_chunk=self._convert_chunk_to_generation_chunk(chunk,default_chunk_class,base_generation_infoifis_first_chunkelse{},)ifgeneration_chunkisNone:continuedefault_chunk_class=generation_chunk.message.__class__logprobs=(generation_chunk.generation_infoor{}).get("logprobs")ifrun_manager:awaitrun_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk,logprobs=logprobs,)is_first_chunk=Falseyieldgeneration_chunkexceptopenai.BadRequestErrorase:_handle_openai_bad_request(e)ifhasattr(response,"get_final_completion")and"response_format"inpayload:final_completion=awaitresponse.get_final_completion()generation_chunk=self._get_generation_chunk_from_completion(final_completion)ifrun_manager:awaitrun_manager.on_llm_new_token(generation_chunk.text,chunk=generation_chunk)yieldgeneration_chunkasyncdef_agenerate(self,messages:List[BaseMessage],stop:Optional[List[str]]=None,run_manager:Optional[AsyncCallbackManagerForLLMRun]=None,**kwargs:Any,)->ChatResult:ifself.streaming:stream_iter=self._astream(messages,stop=stop,run_manager=run_manager,**kwargs)returnawaitagenerate_from_stream(stream_iter)payload=self._get_request_payload(messages,stop=stop,**kwargs)generation_info=Noneif"response_format"inpayload:ifself.include_response_headers:warnings.warn("Cannot currently include response headers when response_format is ""specified.")payload.pop("stream")try:response=awaitself.root_async_client.beta.chat.completions.parse(**payload)exceptopenai.BadRequestErrorase:_handle_openai_bad_request(e)elifself._use_responses_api(payload):original_schema_obj=kwargs.get("response_format")iforiginal_schema_objand_is_pydantic_class(original_schema_obj):response=awaitself.root_async_client.responses.parse(**payload)else:ifself.include_response_headers:raw_response=(awaitself.root_async_client.with_raw_response.responses.create(**payload))response=raw_response.parse()generation_info={"headers":dict(raw_response.headers)}else:response=awaitself.root_async_client.responses.create(**payload)return_construct_lc_result_from_responses_api(response,schema=original_schema_obj,metadata=generation_info)elifself.include_response_headers:raw_response=awaitself.async_client.with_raw_response.create(**payload)response=raw_response.parse()generation_info={"headers":dict(raw_response.headers)}else:response=awaitself.async_client.create(**payload)returnawaitrun_in_executor(None,self._create_chat_result,response,generation_info)@propertydef_identifying_params(self)->Dict[str,Any]:"""Get the identifying parameters."""return{"model_name":self.model_name,**self._default_params}def_get_invocation_params(self,stop:Optional[List[str]]=None,**kwargs:Any)->Dict[str,Any]:"""Get the parameters used to invoke the model."""return{"model":self.model_name,**super()._get_invocation_params(stop=stop),**self._default_params,**kwargs,}def_get_ls_params(self,stop:Optional[List[str]]=None,**kwargs:Any)->LangSmithParams:"""Get standard params for tracing."""params=self._get_invocation_params(stop=stop,**kwargs)ls_params=LangSmithParams(ls_provider="openai",ls_model_name=self.model_name,ls_model_type="chat",ls_temperature=params.get("temperature",self.temperature),)ifls_max_tokens:=params.get("max_tokens",self.max_tokens)orparams.get("max_completion_tokens",self.max_tokens):ls_params["ls_max_tokens"]=ls_max_tokensifls_stop:=stoporparams.get("stop",None):ls_params["ls_stop"]=ls_stopreturnls_params@propertydef_llm_type(self)->str:"""Return type of chat model."""return"openai-chat"def_get_encoding_model(self)->Tuple[str,tiktoken.Encoding]:ifself.tiktoken_model_nameisnotNone:model=self.tiktoken_model_nameelse:model=self.model_nametry:encoding=tiktoken.encoding_for_model(model)exceptKeyError:model="cl100k_base"encoding=tiktoken.get_encoding(model)returnmodel,encoding
[docs]defget_token_ids(self,text:str)->List[int]:"""Get the tokens present in the text with tiktoken package."""ifself.custom_get_token_idsisnotNone:returnself.custom_get_token_ids(text)# tiktoken NOT supported for Python 3.7 or belowifsys.version_info[1]<=7:returnsuper().get_token_ids(text)_,encoding_model=self._get_encoding_model()returnencoding_model.encode(text)
[docs]defget_num_tokens_from_messages(self,messages:List[BaseMessage],tools:Optional[Sequence[Union[Dict[str,Any],Type,Callable,BaseTool]]]=None,)->int:"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package. **Requirements**: 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 BaseTools to be converted to tool schemas. """# TODO: Count bound tools as part of input.iftoolsisnotNone:warnings.warn("Counting tokens in tool schemas is not yet supported. Ignoring tools.")ifsys.version_info[1]<=7:returnsuper().get_num_tokens_from_messages(messages)model,encoding=self._get_encoding_model()ifmodel.startswith("gpt-3.5-turbo-0301"):# every message follows <im_start>{role/name}\n{content}<im_end>\ntokens_per_message=4# if there's a name, the role is omittedtokens_per_name=-1elifmodel.startswith("gpt-3.5-turbo")ormodel.startswith("gpt-4"):tokens_per_message=3tokens_per_name=1else:raiseNotImplementedError(f"get_num_tokens_from_messages() is not presently implemented "f"for model {model}. See ""https://platform.openai.com/docs/guides/text-generation/managing-tokens"# noqa: E501" for information on how messages are converted to tokens.")num_tokens=0messages_dict=[_convert_message_to_dict(m)forminmessages]formessageinmessages_dict:num_tokens+=tokens_per_messageforkey,valueinmessage.items():# This is an inferred approximation. OpenAI does not document how to# count tool message tokens.ifkey=="tool_call_id":num_tokens+=3continueifisinstance(value,list):# content or tool callsforvalinvalue:ifisinstance(val,str)orval["type"]=="text":text=val["text"]ifisinstance(val,dict)elsevalnum_tokens+=len(encoding.encode(text))elifval["type"]=="image_url":ifval["image_url"].get("detail")=="low":num_tokens+=85else:image_size=_url_to_size(val["image_url"]["url"])ifnotimage_size:continuenum_tokens+=_count_image_tokens(*image_size)# Tool/function call token counting is not documented by OpenAI.# This is an approximation.elifval["type"]=="function":num_tokens+=len(encoding.encode(val["function"]["arguments"]))num_tokens+=len(encoding.encode(val["function"]["name"]))else:raiseValueError(f"Unrecognized content block type\n\n{val}")elifnotvalue:continueelse:# Cast str(value) in case the message value is not a string# This occurs with function messagesnum_tokens+=len(encoding.encode(str(value)))ifkey=="name":num_tokens+=tokens_per_name# every reply is primed with <im_start>assistantnum_tokens+=3returnnum_tokens
[docs]@deprecated(since="0.2.1",alternative="langchain_openai.chat_models.base.ChatOpenAI.bind_tools",removal="1.0.0",)defbind_functions(self,functions:Sequence[Union[Dict[str,Any],Type[BaseModel],Callable,BaseTool]],function_call:Optional[Union[_FunctionCall,str,Literal["auto","none"]]]=None,**kwargs:Any,)->Runnable[LanguageModelInput,BaseMessage]:"""Bind functions (and other objects) to this chat model. Assumes model is compatible with OpenAI function-calling API. NOTE: Using bind_tools is recommended instead, as the `functions` and `function_call` request parameters are officially marked as deprecated by OpenAI. Args: functions: A list of function definitions to bind to this chat model. Can be a dictionary, pydantic model, or callable. Pydantic models and callables will be automatically converted to their schema dictionary representation. function_call: Which function to require the model to call. Must be the name of the single provided function or "auto" to automatically determine which function to call (if any). **kwargs: Any additional parameters to pass to the :class:`~langchain.runnable.Runnable` constructor. """formatted_functions=[convert_to_openai_function(fn)forfninfunctions]iffunction_callisnotNone:function_call=({"name":function_call}ifisinstance(function_call,str)andfunction_callnotin("auto","none")elsefunction_call)ifisinstance(function_call,dict)andlen(formatted_functions)!=1:raiseValueError("When specifying `function_call`, you must provide exactly one ""function.")if(isinstance(function_call,dict)andformatted_functions[0]["name"]!=function_call["name"]):raiseValueError(f"Function call {function_call} was specified, but the only "f"provided function was {formatted_functions[0]['name']}.")kwargs={**kwargs,"function_call":function_call}returnsuper().bind(functions=formatted_functions,**kwargs)
[docs]defbind_tools(self,tools:Sequence[Union[Dict[str,Any],Type,Callable,BaseTool]],*,tool_choice:Optional[Union[dict,str,Literal["auto","none","required","any"],bool]]=None,strict:Optional[bool]=None,parallel_tool_calls:Optional[bool]=None,**kwargs:Any,)->Runnable[LanguageModelInput,BaseMessage]:"""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 :meth:`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. If True, the input schema will be validated according to https://platform.openai.com/docs/guides/structured-outputs/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). kwargs: Any additional parameters are passed directly to :meth:`~langchain_openai.chat_models.base.ChatOpenAI.bind`. .. versionchanged:: 0.1.21 Support for ``strict`` argument added. """# noqa: E501ifparallel_tool_callsisnotNone:kwargs["parallel_tool_calls"]=parallel_tool_callsformatted_tools=[convert_to_openai_tool(tool,strict=strict)fortoolintools]tool_names=[]fortoolinformatted_tools:if"function"intool:tool_names.append(tool["function"]["name"])elif"name"intool:tool_names.append(tool["name"])else:passiftool_choice:ifisinstance(tool_choice,str):# tool_choice is a tool/function nameiftool_choiceintool_names:tool_choice={"type":"function","function":{"name":tool_choice},}eliftool_choicein("file_search","web_search_preview","computer_use_preview",):tool_choice={"type":tool_choice}# 'any' is not natively supported by OpenAI API.# We support 'any' since other models use this instead of 'required'.eliftool_choice=="any":tool_choice="required"else:passelifisinstance(tool_choice,bool):tool_choice="required"elifisinstance(tool_choice,dict):passelse:raiseValueError(f"Unrecognized tool_choice type. Expected str, bool or dict. "f"Received: {tool_choice}")kwargs["tool_choice"]=tool_choicereturnsuper().bind(tools=formatted_tools,**kwargs)
[docs]defwith_structured_output(self,schema:Optional[_DictOrPydanticClass]=None,*,method:Literal["function_calling","json_mode","json_schema"]="function_calling",include_raw:bool=False,strict:Optional[bool]=None,**kwargs:Any,)->Runnable[LanguageModelInput,_DictOrPydantic]:"""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 (support added in 0.1.20), - 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 :meth:`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 (formerly called function calling) API: https://platform.openai.com/docs/guides/function-calling - "json_schema": Uses OpenAI's Structured Output API: https://platform.openai.com/docs/guides/structured-outputs Supported for "gpt-4o-mini", "gpt-4o-2024-08-06", "o1", and later models. - "json_mode": Uses OpenAI's 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: https://platform.openai.com/docs/guides/structured-outputs/json-mode Learn more about the differences between the methods and which models support which methods here: - https://platform.openai.com/docs/guides/structured-outputs/structured-outputs-vs-json-mode - 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 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. kwargs: Additional keyword args aren't supported. Returns: A Runnable that takes same inputs as a :class:`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": Optional[BaseException] .. versionchanged:: 0.1.20 Added support for TypedDict class ``schema``. .. versionchanged:: 0.1.21 Support for ``strict`` argument added. Support for ``method`` = "json_schema" added. """# noqa: E501ifkwargs:raiseValueError(f"Received unsupported arguments {kwargs}")ifstrictisnotNoneandmethod=="json_mode":raiseValueError("Argument `strict` is not supported with `method`='json_mode'")is_pydantic_schema=_is_pydantic_class(schema)ifmethod=="json_schema":# Check for Pydantic BaseModel V1if(is_pydantic_schemaandissubclass(schema,BaseModelV1)# type: ignore[arg-type]):warnings.warn("Received 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".')method="function_calling"# Check for incompatible modelifself.model_nameand(self.model_name.startswith("gpt-3")orself.model_name.startswith("gpt-4-")orself.model_name=="gpt-4"):warnings.warn(f"Cannot use method='json_schema' with model {self.model_name} "f"since it doesn't support OpenAI's Structured Output API. You can "f"see supported models here: "f"https://platform.openai.com/docs/guides/structured-outputs#supported-models. "# noqa: E501"To fix this warning, set `method='function_calling'. ""Overriding to method='function_calling'.")method="function_calling"ifmethod=="function_calling":ifschemaisNone:raiseValueError("schema must be specified when method is not 'json_mode'. ""Received None.")tool_name=convert_to_openai_tool(schema)["function"]["name"]bind_kwargs=self._filter_disabled_params(tool_choice=tool_name,parallel_tool_calls=False,strict=strict,ls_structured_output_format={"kwargs":{"method":method,"strict":strict},"schema":schema,},)llm=self.bind_tools([schema],**bind_kwargs)ifis_pydantic_schema:output_parser:Runnable=PydanticToolsParser(tools=[schema],# type: ignore[list-item]first_tool_only=True,# type: ignore[list-item])else:output_parser=JsonOutputKeyToolsParser(key_name=tool_name,first_tool_only=True)elifmethod=="json_mode":llm=self.bind(response_format={"type":"json_object"},ls_structured_output_format={"kwargs":{"method":method},"schema":schema,},)output_parser=(PydanticOutputParser(pydantic_object=schema)# type: ignore[arg-type]ifis_pydantic_schemaelseJsonOutputParser())elifmethod=="json_schema":ifschemaisNone:raiseValueError("schema must be specified when method is not 'json_mode'. ""Received None.")response_format=_convert_to_openai_response_format(schema,strict=strict)llm=self.bind(response_format=response_format,ls_structured_output_format={"kwargs":{"method":method,"strict":strict},"schema":convert_to_openai_tool(schema),},)ifis_pydantic_schema:output_parser=RunnableLambda(partial(_oai_structured_outputs_parser,schema=cast(type,schema))).with_types(output_type=cast(type,schema))else:output_parser=JsonOutputParser()else:raiseValueError(f"Unrecognized method argument. Expected one of 'function_calling' or "f"'json_mode'. Received: '{method}'")ifinclude_raw:parser_assign=RunnablePassthrough.assign(parsed=itemgetter("raw")|output_parser,parsing_error=lambda_:None)parser_none=RunnablePassthrough.assign(parsed=lambda_:None)parser_with_fallback=parser_assign.with_fallbacks([parser_none],exception_key="parsing_error")returnRunnableMap(raw=llm)|parser_with_fallbackelse:returnllm|output_parser
def_filter_disabled_params(self,**kwargs:Any)->Dict[str,Any]:ifnotself.disabled_params:returnkwargsfiltered={}fork,vinkwargs.items():# Skip paramifkinself.disabled_paramsand(self.disabled_params[k]isNoneorvinself.disabled_params[k]):continue# Keep paramelse:filtered[k]=vreturnfiltereddef_get_generation_chunk_from_completion(self,completion:openai.BaseModel)->ChatGenerationChunk:"""Get chunk from completion (e.g., from final completion of a stream)."""chat_result=self._create_chat_result(completion)chat_message=chat_result.generations[0].messageifisinstance(chat_message,AIMessage):usage_metadata=chat_message.usage_metadata# Skip tool_calls, already sent as chunksif"tool_calls"inchat_message.additional_kwargs:chat_message.additional_kwargs.pop("tool_calls")else:usage_metadata=Nonemessage=AIMessageChunk(content="",additional_kwargs=chat_message.additional_kwargs,usage_metadata=usage_metadata,)returnChatGenerationChunk(message=message,generation_info=chat_result.llm_output)
[docs]classChatOpenAI(BaseChatOpenAI):# type: ignore[override]"""OpenAI chat model integration. .. dropdown:: Setup :open: Install ``langchain-openai`` and set environment variable ``OPENAI_API_KEY``. .. code-block:: bash pip install -U langchain-openai export OPENAI_API_KEY="your-api-key" .. dropdown:: Key init args — completion params model: str Name of OpenAI model to use. temperature: float Sampling temperature. max_tokens: Optional[int] Max number of tokens to generate. logprobs: Optional[bool] Whether to return logprobs. stream_options: Dict Configure streaming outputs, like whether to return token usage when streaming (``{"include_usage": True}``). use_responses_api: Optional[bool] Whether to use the responses API. See full list of supported init args and their descriptions in the params section. .. dropdown:: Key init args — client params timeout: Union[float, Tuple[float, float], Any, None] Timeout for requests. max_retries: Optional[int] Max number of retries. api_key: Optional[str] OpenAI API key. If not passed in will be read from env var OPENAI_API_KEY. base_url: Optional[str] Base URL for API requests. Only specify if using a proxy or service emulator. organization: Optional[str] 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 in the params section. .. dropdown:: Instantiate .. code-block:: python from langchain_openai import ChatOpenAI llm = ChatOpenAI( model="gpt-4o", temperature=0, max_tokens=None, timeout=None, max_retries=2, # api_key="...", # base_url="...", # organization="...", # other params... ) **NOTE**: Any param which is not explicitly supported will be passed directly to the ``openai.OpenAI.chat.completions.create(...)`` API every time to the model is invoked. For example: .. code-block:: 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) .. dropdown:: Invoke .. code-block:: python messages = [ ( "system", "You are a helpful translator. Translate the user sentence to French.", ), ("human", "I love programming."), ] llm.invoke(messages) .. code-block:: pycon 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}, ) .. dropdown:: Stream .. code-block:: python for chunk in llm.stream(messages): print(chunk.text(), end="") .. code-block:: 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", ) .. code-block:: python stream = llm.stream(messages) full = next(stream) for chunk in stream: full += chunk full .. code-block:: python AIMessageChunk( content="J'adore la programmation.", response_metadata={"finish_reason": "stop"}, id="run-bf917526-7f58-4683-84f7-36a6b671d140", ) .. dropdown:: Async .. code-block:: python await llm.ainvoke(messages) # stream: # async for chunk in (await llm.astream(messages)) # batch: # await llm.abatch([messages]) .. code-block:: 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}, ) .. dropdown:: Tool calling .. code-block:: 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" ) llm_with_tools = llm.bind_tools( [GetWeather, GetPopulation] # strict = True # enforce tool args schema is respected ) ai_msg = llm_with_tools.invoke( "Which city is hotter today and which is bigger: LA or NY?" ) ai_msg.tool_calls .. code-block:: 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 that ``openai >= 1.32`` supports a ``parallel_tool_calls`` parameter that defaults to ``True``. This parameter can be set to ``False`` to disable parallel tool calls: .. code-block:: python ai_msg = llm_with_tools.invoke( "What is the weather in LA and NY?", parallel_tool_calls=False ) ai_msg.tool_calls .. code-block:: 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 ``llm.bind(parallel_tool_calls=False)`` or during instantiation by setting ``model_kwargs``. See ``ChatOpenAI.bind_tools()`` method for more. .. dropdown:: Built-in tools .. versionadded:: 0.3.9 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://python.langchain.com/docs/integrations/chat/openai/>`_ for more detail. .. code-block:: python from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o-mini") tool = {"type": "web_search_preview"} llm_with_tools = llm.bind_tools([tool]) response = llm_with_tools.invoke("What was a positive news story from today?") response.content .. code-block:: 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>", } ], } ] .. dropdown:: Managing conversation state .. versionadded:: 0.3.9 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. See LangChain `docs <https://python.langchain.com/docs/integrations/chat/openai/>`_ for more detail. .. code-block:: python from langchain_openai import ChatOpenAI llm = ChatOpenAI(model="gpt-4o-mini", use_responses_api=True) response = llm.invoke("Hi, I'm Bob.") response.text() .. code-block:: python "Hi Bob! How can I assist you today?" .. code-block:: python second_response = llm.invoke( "What is my name?", previous_response_id=response.response_metadata["id"] ) second_response.text() .. code-block:: python "Your name is Bob. How can I help you today, Bob?" .. dropdown:: Structured output .. code-block:: python from typing import Optional 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: Optional[int] = Field(description="How funny the joke is, from 1 to 10") structured_llm = llm.with_structured_output(Joke) structured_llm.invoke("Tell me a joke about cats") .. code-block:: python Joke( setup="Why was the cat sitting on the computer?", punchline="To keep an eye on the mouse!", rating=None, ) See ``ChatOpenAI.with_structured_output()`` for more. .. dropdown:: JSON mode .. code-block:: python json_llm = llm.bind(response_format={"type": "json_object"}) ai_msg = json_llm.invoke( "Return a JSON object with key 'random_ints' and a value of 10 random ints in [0-99]" ) ai_msg.content .. code-block:: python '\\n{\\n "random_ints": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]\\n}' .. dropdown:: Image input .. code-block:: python import base64 import httpx from langchain_core.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 = llm.invoke([message]) ai_msg.content .. code-block:: python "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." .. dropdown:: Token usage .. code-block:: python ai_msg = llm.invoke(messages) ai_msg.usage_metadata .. code-block:: python {"input_tokens": 28, "output_tokens": 5, "total_tokens": 33} When streaming, set the ``stream_usage`` kwarg: .. code-block:: python stream = llm.stream(messages, stream_usage=True) full = next(stream) for chunk in stream: full += chunk full.usage_metadata .. code-block:: python {"input_tokens": 28, "output_tokens": 5, "total_tokens": 33} Alternatively, setting ``stream_usage`` when instantiating the model can be useful when incorporating ``ChatOpenAI`` into LCEL chains-- or when using methods like ``.with_structured_output``, which generate chains under the hood. .. code-block:: python llm = ChatOpenAI(model="gpt-4o", stream_usage=True) structured_llm = llm.with_structured_output(...) .. dropdown:: Logprobs .. code-block:: python logprobs_llm = llm.bind(logprobs=True) ai_msg = logprobs_llm.invoke(messages) ai_msg.response_metadata["logprobs"] .. code-block:: python { "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": [], }, ] } .. dropdown:: Response metadata .. code-block:: python ai_msg = llm.invoke(messages) ai_msg.response_metadata .. code-block:: python { "token_usage": { "completion_tokens": 5, "prompt_tokens": 28, "total_tokens": 33, }, "model_name": "gpt-4o", "system_fingerprint": "fp_319be4768e", "finish_reason": "stop", "logprobs": None, } """# noqa: E501stream_usage:bool=False"""Whether to include usage metadata in streaming output. If True, additional message chunks will be generated during the stream including usage metadata. """max_tokens:Optional[int]=Field(default=None,alias="max_completion_tokens")"""Maximum number of tokens to generate."""@propertydeflc_secrets(self)->Dict[str,str]:return{"openai_api_key":"OPENAI_API_KEY"}@classmethoddefget_lc_namespace(cls)->List[str]:"""Get the namespace of the langchain object."""return["langchain","chat_models","openai"]@propertydeflc_attributes(self)->Dict[str,Any]:attributes:Dict[str,Any]={}ifself.openai_organization:attributes["openai_organization"]=self.openai_organizationifself.openai_api_base:attributes["openai_api_base"]=self.openai_api_baseifself.openai_proxy:attributes["openai_proxy"]=self.openai_proxyreturnattributes@classmethoddefis_lc_serializable(cls)->bool:"""Return whether this model can be serialized by Langchain."""returnTrue@propertydef_default_params(self)->Dict[str,Any]:"""Get the default parameters for calling OpenAI API."""params=super()._default_paramsif"max_tokens"inparams:params["max_completion_tokens"]=params.pop("max_tokens")returnparamsdef_get_request_payload(self,input_:LanguageModelInput,*,stop:Optional[List[str]]=None,**kwargs:Any,)->dict:payload=super()._get_request_payload(input_,stop=stop,**kwargs)# max_tokens was deprecated in favor of max_completion_tokens# in September 2024 releaseif"max_tokens"inpayload:payload["max_completion_tokens"]=payload.pop("max_tokens")# Mutate system message role to "developer" for o-series modelsifself.model_nameandre.match(r"^o\d",self.model_name):formessageinpayload.get("messages",[]):ifmessage["role"]=="system":message["role"]="developer"returnpayloaddef_should_stream_usage(self,stream_usage:Optional[bool]=None,**kwargs:Any)->bool:"""Determine 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_usage_sources=[# order of preferencestream_usage,kwargs.get("stream_options",{}).get("include_usage"),self.model_kwargs.get("stream_options",{}).get("include_usage"),self.stream_usage,]forsourceinstream_usage_sources:ifisinstance(source,bool):returnsourcereturnself.stream_usagedef_stream(self,*args:Any,stream_usage:Optional[bool]=None,**kwargs:Any)->Iterator[ChatGenerationChunk]:"""Set default stream_options."""ifself._use_responses_api({**kwargs,**self.model_kwargs}):returnsuper()._stream_responses(*args,**kwargs)else:stream_usage=self._should_stream_usage(stream_usage,**kwargs)# Note: stream_options is not a valid parameter for Azure OpenAI.# To support users proxying Azure through ChatOpenAI, here we only specify# stream_options if include_usage is set to True.# See https://learn.microsoft.com/en-us/azure/ai-services/openai/whats-new# for release notes.ifstream_usage:kwargs["stream_options"]={"include_usage":stream_usage}returnsuper()._stream(*args,**kwargs)asyncdef_astream(self,*args:Any,stream_usage:Optional[bool]=None,**kwargs:Any)->AsyncIterator[ChatGenerationChunk]:"""Set default stream_options."""ifself._use_responses_api({**kwargs,**self.model_kwargs}):asyncforchunkinsuper()._astream_responses(*args,**kwargs):yieldchunkelse:stream_usage=self._should_stream_usage(stream_usage,**kwargs)ifstream_usage:kwargs["stream_options"]={"include_usage":stream_usage}asyncforchunkinsuper()._astream(*args,**kwargs):yieldchunk
[docs]defwith_structured_output(self,schema:Optional[_DictOrPydanticClass]=None,*,method:Literal["function_calling","json_mode","json_schema"]="json_schema",include_raw:bool=False,strict:Optional[bool]=None,**kwargs:Any,)->Runnable[LanguageModelInput,_DictOrPydantic]:"""Model wrapper that returns outputs formatted to match the given schema. Args: schema: The output schema. Can be passed in as: - a JSON Schema, - a TypedDict class, - or a Pydantic class, - an OpenAI function/tool schema. 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 :meth:`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 Supported for "gpt-4o-mini", "gpt-4o-2024-08-06", "o1", and later models. - "function_calling": Uses OpenAI's tool-calling (formerly called function calling) API: https://platform.openai.com/docs/guides/function-calling - "json_mode": Uses OpenAI's 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: https://platform.openai.com/docs/guides/structured-outputs/json-mode Learn more about the differences between the methods and which models support which methods here: - https://platform.openai.com/docs/guides/structured-outputs/structured-outputs-vs-json-mode - 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 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"``. kwargs: Additional keyword args aren't supported. Returns: A Runnable that takes same inputs as a :class:`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": Optional[BaseException] .. versionchanged:: 0.1.20 Added support for TypedDict class ``schema``. .. versionchanged:: 0.1.21 Support for ``strict`` argument added. Support for ``method="json_schema"`` added. .. versionchanged:: 0.3.0 ``method`` default changed from "function_calling" to "json_schema". .. dropdown:: 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 here: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas .. code-block:: python from typing import Optional 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: Optional[str] = Field( default=..., description="A justification for the answer." ) llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output(AnswerWithJustification) structured_llm.invoke( "What weighs more a pound of bricks or a pound of feathers" ) # -> 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.' # ) .. dropdown:: Example: schema=Pydantic class, method="function_calling", include_raw=False, strict=False .. code-block:: python from typing import Optional 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: Optional[str] = Field( default=..., description="A justification for the answer." ) llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output( AnswerWithJustification, method="function_calling" ) structured_llm.invoke( "What weighs more a pound of bricks or a pound of feathers" ) # -> 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.' # ) .. dropdown:: Example: schema=Pydantic class, method="json_schema", include_raw=True .. code-block:: 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 llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output( AnswerWithJustification, include_raw=True ) structured_llm.invoke( "What weighs more a pound of bricks or a pound of feathers" ) # -> { # '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 # } .. dropdown:: Example: schema=TypedDict class, method="json_schema", include_raw=False, strict=False .. code-block:: python # IMPORTANT: If you are using Python <=3.8, you need to import Annotated # from typing_extensions, not from typing. 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[ Optional[str], None, "A justification for the answer." ] llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output(AnswerWithJustification) structured_llm.invoke( "What weighs more a pound of bricks or a pound of feathers" ) # -> { # '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.' # } .. dropdown:: Example: schema=OpenAI function schema, method="json_schema", include_raw=False .. code-block:: 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'] } } llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output(oai_schema) structured_llm.invoke( "What weighs more a pound of bricks or a pound of feathers" ) # -> { # '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.' # } .. dropdown:: Example: schema=Pydantic class, method="json_mode", include_raw=True .. code-block:: from langchain_openai import ChatOpenAI from pydantic import BaseModel class AnswerWithJustification(BaseModel): answer: str justification: str llm = ChatOpenAI(model="gpt-4o", temperature=0) structured_llm = llm.with_structured_output( AnswerWithJustification, method="json_mode", include_raw=True ) structured_llm.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?" ) # -> { # '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 # } .. dropdown:: Example: schema=None, method="json_mode", include_raw=True .. code-block:: structured_llm = llm.with_structured_output(method="json_mode", include_raw=True) structured_llm.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?" ) # -> { # '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 # } """# noqa: E501returnsuper().with_structured_output(schema,method=method,include_raw=include_raw,strict=strict,**kwargs)
def_is_pydantic_class(obj:Any)->bool:returnisinstance(obj,type)andis_basemodel_subclass(obj)def_lc_tool_call_to_openai_tool_call(tool_call:ToolCall)->dict:return{"type":"function","id":tool_call["id"],"function":{"name":tool_call["name"],"arguments":json.dumps(tool_call["args"]),},}def_lc_invalid_tool_call_to_openai_tool_call(invalid_tool_call:InvalidToolCall,)->dict:return{"type":"function","id":invalid_tool_call["id"],"function":{"name":invalid_tool_call["name"],"arguments":invalid_tool_call["args"],},}def_url_to_size(image_source:str)->Optional[Tuple[int,int]]:try:fromPILimportImage# type: ignore[import]exceptImportError:logger.info("Unable to count image tokens. To count image tokens please install ""`pip install -U pillow httpx`.")returnNoneif_is_url(image_source):try:importhttpxexceptImportError:logger.info("Unable to count image tokens. To count image tokens please install ""`pip install -U httpx`.")returnNoneresponse=httpx.get(image_source)response.raise_for_status()width,height=Image.open(BytesIO(response.content)).sizereturnwidth,heightelif_is_b64(image_source):_,encoded=image_source.split(",",1)data=base64.b64decode(encoded)width,height=Image.open(BytesIO(data)).sizereturnwidth,heightelse:returnNonedef_count_image_tokens(width:int,height:int)->int:# Reference: https://platform.openai.com/docs/guides/vision/calculating-costswidth,height=_resize(width,height)h=ceil(height/512)w=ceil(width/512)return(170*h*w)+85def_is_url(s:str)->bool:try:result=urlparse(s)returnall([result.scheme,result.netloc])exceptExceptionase:logger.debug(f"Unable to parse URL: {e}")returnFalsedef_is_b64(s:str)->bool:returns.startswith("data:image")def_resize(width:int,height:int)->Tuple[int,int]:# larger side must be <= 2048ifwidth>2048orheight>2048:ifwidth>height:height=(height*2048)//widthwidth=2048else:width=(width*2048)//heightheight=2048# smaller side must be <= 768ifwidth>768andheight>768:ifwidth>height:width=(width*768)//heightheight=768else:height=(width*768)//heightwidth=768returnwidth,heightdef_convert_to_openai_response_format(schema:Union[Dict[str,Any],Type],*,strict:Optional[bool]=None)->Union[Dict,TypeBaseModel]:ifisinstance(schema,type)andis_basemodel_subclass(schema):returnschemaif(isinstance(schema,dict)and"json_schema"inschemaandschema.get("type")=="json_schema"):response_format=schemaelifisinstance(schema,dict)and"name"inschemaand"schema"inschema:response_format={"type":"json_schema","json_schema":schema}else:ifstrictisNone:ifisinstance(schema,dict)andisinstance(schema.get("strict"),bool):strict=schema["strict"]else:strict=Falsefunction=convert_to_openai_function(schema,strict=strict)function["schema"]=function.pop("parameters")response_format={"type":"json_schema","json_schema":function}ifstrictisnotNoneandstrictisnotresponse_format["json_schema"].get("strict"):msg=(f"Output schema already has 'strict' value set to "f"{schema['json_schema']['strict']} but 'strict' also passed in to "f"with_structured_output as {strict}. Please make sure that "f"'strict' is only specified in one place.")raiseValueError(msg)returnresponse_formatdef_oai_structured_outputs_parser(ai_msg:AIMessage,schema:Type[_BM])->PydanticBaseModel:ifparsed:=ai_msg.additional_kwargs.get("parsed"):ifisinstance(parsed,dict):returnschema(**parsed)else:returnparsedelifai_msg.additional_kwargs.get("refusal"):raiseOpenAIRefusalError(ai_msg.additional_kwargs["refusal"])else:raiseValueError("Structured Output response does not have a 'parsed' field nor a 'refusal' "f"field. Received message:\n\n{ai_msg}")
[docs]classOpenAIRefusalError(Exception):"""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 here for more on refusals: https://platform.openai.com/docs/guides/structured-outputs/refusals .. versionadded:: 0.1.21 """
def_create_usage_metadata(oai_token_usage:dict)->UsageMetadata:input_tokens=oai_token_usage.get("prompt_tokens",0)output_tokens=oai_token_usage.get("completion_tokens",0)total_tokens=oai_token_usage.get("total_tokens",input_tokens+output_tokens)input_token_details:dict={"audio":(oai_token_usage.get("prompt_tokens_details")or{}).get("audio_tokens"),"cache_read":(oai_token_usage.get("prompt_tokens_details")or{}).get("cached_tokens"),}output_token_details:dict={"audio":(oai_token_usage.get("completion_tokens_details")or{}).get("audio_tokens"),"reasoning":(oai_token_usage.get("completion_tokens_details")or{}).get("reasoning_tokens"),}returnUsageMetadata(input_tokens=input_tokens,output_tokens=output_tokens,total_tokens=total_tokens,input_token_details=InputTokenDetails(**{k:vfork,vininput_token_details.items()ifvisnotNone}),output_token_details=OutputTokenDetails(**{k:vfork,vinoutput_token_details.items()ifvisnotNone}),)def_create_usage_metadata_responses(oai_token_usage:dict)->UsageMetadata:input_tokens=oai_token_usage.get("input_tokens",0)output_tokens=oai_token_usage.get("output_tokens",0)total_tokens=oai_token_usage.get("total_tokens",input_tokens+output_tokens)output_token_details:dict={"audio":(oai_token_usage.get("completion_tokens_details")or{}).get("audio_tokens"),"reasoning":(oai_token_usage.get("output_token_details")or{}).get("reasoning_tokens"),}returnUsageMetadata(input_tokens=input_tokens,output_tokens=output_tokens,total_tokens=total_tokens,output_token_details=OutputTokenDetails(**{k:vfork,vinoutput_token_details.items()ifvisnotNone}),)def_is_builtin_tool(tool:dict)->bool:return"type"intoolandtool["type"]!="function"def_use_responses_api(payload:dict)->bool:uses_builtin_tools="tools"inpayloadandany(_is_builtin_tool(tool)fortoolinpayload["tools"])responses_only_args={"previous_response_id","text","truncation","include"}returnbool(uses_builtin_toolsorresponses_only_args.intersection(payload))def_construct_responses_api_payload(messages:Sequence[BaseMessage],payload:dict)->dict:# Rename legacy parametersforlegacy_token_paramin["max_tokens","max_completion_tokens"]:iflegacy_token_paraminpayload:payload["max_output_tokens"]=payload.pop(legacy_token_param)if"reasoning_effort"inpayload:payload["reasoning"]={"effort":payload.pop("reasoning_effort")}payload["input"]=_construct_responses_api_input(messages)iftools:=payload.pop("tools",None):new_tools:list=[]fortoolintools:# chat api: {"type": "function", "function": {"name": "...", "description": "...", "parameters": {...}, "strict": ...}} # noqa: E501# responses api: {"type": "function", "name": "...", "description": "...", "parameters": {...}, "strict": ...} # noqa: E501iftool["type"]=="function"and"function"intool:new_tools.append({"type":"function",**tool["function"]})else:new_tools.append(tool)payload["tools"]=new_toolsiftool_choice:=payload.pop("tool_choice",None):# chat api: {"type": "function", "function": {"name": "..."}}# responses api: {"type": "function", "name": "..."}if(isinstance(tool_choice,dict)andtool_choice["type"]=="function"and"function"intool_choice):payload["tool_choice"]={"type":"function",**tool_choice["function"]}else:payload["tool_choice"]=tool_choice# Structured outputifschema:=payload.pop("response_format",None):ifpayload.get("text"):text=payload["text"]raiseValueError("Can specify at most one of 'response_format' or 'text', received both:"f"\n{schema=}\n{text=}")# For pydantic + non-streaming case, we use responses.parse.# Otherwise, we use responses.create.strict=payload.pop("strict",None)ifnotpayload.get("stream")and_is_pydantic_class(schema):payload["text_format"]=schemaelse:if_is_pydantic_class(schema):schema_dict=schema.model_json_schema()strict=Trueelse:schema_dict=schemaifschema_dict=={"type":"json_object"}:# JSON modepayload["text"]={"format":{"type":"json_object"}}elif((response_format:=_convert_to_openai_response_format(schema_dict,strict=strict))and(isinstance(response_format,dict))and(response_format["type"]=="json_schema")):payload["text"]={"format":{"type":"json_schema",**response_format["json_schema"]}}else:passreturnpayloaddef_make_computer_call_output_from_message(message:ToolMessage)->dict:computer_call_output:dict={"call_id":message.tool_call_id,"type":"computer_call_output",}ifisinstance(message.content,list):# Use first input_image blockoutput=next(blockforblockinmessage.contentifcast(dict,block)["type"]=="input_image")else:# string, assume image_urloutput={"type":"input_image","image_url":message.content}computer_call_output["output"]=outputreturncomputer_call_outputdef_construct_responses_api_input(messages:Sequence[BaseMessage])->list:input_=[]forlc_msginmessages:msg=_convert_message_to_dict(lc_msg)# "name" parameter unsupportedif"name"inmsg:msg.pop("name")ifmsg["role"]=="tool":tool_output=msg["content"]iflc_msg.additional_kwargs.get("type")=="computer_call_output":computer_call_output=_make_computer_call_output_from_message(cast(ToolMessage,lc_msg))input_.append(computer_call_output)else:ifnotisinstance(tool_output,str):tool_output=_stringify(tool_output)function_call_output={"type":"function_call_output","output":tool_output,"call_id":msg["tool_call_id"],}input_.append(function_call_output)elifmsg["role"]=="assistant":# Reasoning itemsreasoning_items=[]ifreasoning:=lc_msg.additional_kwargs.get("reasoning"):reasoning_items.append(reasoning)# Function callsfunction_calls=[]iftool_calls:=msg.pop("tool_calls",None):# TODO: should you be able to preserve the function call object id on# the langchain tool calls themselves?function_call_ids=lc_msg.additional_kwargs.get(_FUNCTION_CALL_IDS_MAP_KEY)fortool_callintool_calls:function_call={"type":"function_call","name":tool_call["function"]["name"],"arguments":tool_call["function"]["arguments"],"call_id":tool_call["id"],}iffunction_call_idsisnotNoneand(_id:=function_call_ids.get(tool_call["id"])):function_call["id"]=_idfunction_calls.append(function_call)# Computer callscomputer_calls=[]tool_outputs=lc_msg.additional_kwargs.get("tool_outputs",[])fortool_outputintool_outputs:iftool_output.get("type")=="computer_call":computer_calls.append(tool_output)msg["content"]=msg.get("content")or[]iflc_msg.additional_kwargs.get("refusal"):ifisinstance(msg["content"],str):msg["content"]=[{"type":"output_text","text":msg["content"],"annotations":[],}]msg["content"]=msg["content"]+[{"type":"refusal","refusal":lc_msg.additional_kwargs["refusal"]}]ifisinstance(msg["content"],list):new_blocks=[]forblockinmsg["content"]:# chat api: {"type": "text", "text": "..."}# responses api: {"type": "output_text", "text": "...", "annotations": [...]} # noqa: E501ifblock["type"]=="text":new_blocks.append({"type":"output_text","text":block["text"],"annotations":block.get("annotations")or[],})elifblock["type"]in("output_text","refusal"):new_blocks.append(block)else:passmsg["content"]=new_blocksifmsg["content"]:input_.append(msg)input_.extend(reasoning_items)input_.extend(function_calls)input_.extend(computer_calls)elifmsg["role"]=="user":ifisinstance(msg["content"],list):new_blocks=[]forblockinmsg["content"]:# chat api: {"type": "text", "text": "..."}# responses api: {"type": "input_text", "text": "..."}ifblock["type"]=="text":new_blocks.append({"type":"input_text","text":block["text"]})# chat api: {"type": "image_url", "image_url": {"url": "...", "detail": "..."}} # noqa: E501# responses api: {"type": "image_url", "image_url": "...", "detail": "...", "file_id": "..."} # noqa: E501elifblock["type"]=="image_url":new_block={"type":"input_image","image_url":block["image_url"]["url"],}ifblock["image_url"].get("detail"):new_block["detail"]=block["image_url"]["detail"]new_blocks.append(new_block)elifblock["type"]in("input_text","input_image","input_file"):new_blocks.append(block)else:passmsg["content"]=new_blocksinput_.append(msg)else:input_.append(msg)returninput_def_construct_lc_result_from_responses_api(response:Response,schema:Optional[Type[_BM]]=None,metadata:Optional[dict]=None,)->ChatResult:"""Construct ChatResponse from OpenAI Response API response."""ifresponse.error:raiseValueError(response.error)response_metadata={k:vfork,vinresponse.model_dump(exclude_none=True,mode="json").items()ifkin("created_at","id","incomplete_details","metadata","object","status","user","model",)}ifmetadata:response_metadata.update(metadata)# for compatibility with chat completion calls.response_metadata["model_name"]=response_metadata.get("model")ifresponse.usage:usage_metadata=_create_usage_metadata_responses(response.usage.model_dump())else:usage_metadata=Nonecontent_blocks:list=[]tool_calls=[]invalid_tool_calls=[]additional_kwargs:dict={}msg_id=Noneforoutputinresponse.output:ifoutput.type=="message":forcontentinoutput.content:ifcontent.type=="output_text":block={"type":"text","text":content.text,"annotations":[annotation.model_dump()forannotationincontent.annotations],}content_blocks.append(block)ifhasattr(content,"parsed"):additional_kwargs["parsed"]=content.parsedifcontent.type=="refusal":additional_kwargs["refusal"]=content.refusalmsg_id=output.idelifoutput.type=="function_call":try:args=json.loads(output.arguments,strict=False)error=NoneexceptJSONDecodeErrorase:args=output.argumentserror=str(e)iferrorisNone:tool_call={"type":"tool_call","name":output.name,"args":args,"id":output.call_id,}tool_calls.append(tool_call)else:tool_call={"type":"invalid_tool_call","name":output.name,"args":args,"id":output.call_id,"error":error,}invalid_tool_calls.append(tool_call)if_FUNCTION_CALL_IDS_MAP_KEYnotinadditional_kwargs:additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY]={}additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY][output.call_id]=output.idelifoutput.type=="reasoning":additional_kwargs["reasoning"]=output.model_dump(exclude_none=True,mode="json")else:tool_output=output.model_dump(exclude_none=True,mode="json")if"tool_outputs"inadditional_kwargs:additional_kwargs["tool_outputs"].append(tool_output)else:additional_kwargs["tool_outputs"]=[tool_output]# Workaround for parsing structured output in the streaming case.# from openai import OpenAI# from pydantic import BaseModel# class Foo(BaseModel):# response: str# client = OpenAI()# client.responses.parse(# model="gpt-4o-mini",# input=[{"content": "how are ya", "role": "user"}],# text_format=Foo,# stream=True, # <-- errors# )if(schemaisnotNoneand"parsed"notinadditional_kwargsandresponse.output_text# tool calls can generate empty output textandresponse.textand(text_config:=response.text.model_dump())and(format_:=text_config.get("format",{}))and(format_.get("type")=="json_schema")):try:parsed_dict=json.loads(response.output_text)ifschemaand_is_pydantic_class(schema):parsed=schema(**parsed_dict)else:parsed=parsed_dictadditional_kwargs["parsed"]=parsedexceptjson.JSONDecodeError:passmessage=AIMessage(content=content_blocks,id=msg_id,usage_metadata=usage_metadata,response_metadata=response_metadata,additional_kwargs=additional_kwargs,tool_calls=tool_calls,invalid_tool_calls=invalid_tool_calls,)returnChatResult(generations=[ChatGeneration(message=message)])def_convert_responses_chunk_to_generation_chunk(chunk:Any,schema:Optional[Type[_BM]]=None,metadata:Optional[dict]=None)->Optional[ChatGenerationChunk]:content=[]tool_call_chunks:list=[]additional_kwargs:dict={}ifmetadata:response_metadata=metadataelse:response_metadata={}usage_metadata=Noneid=Noneifchunk.type=="response.output_text.delta":content.append({"type":"text","text":chunk.delta,"index":chunk.content_index})elifchunk.type=="response.output_text.annotation.added":content.append({"annotations":[chunk.annotation.model_dump(exclude_none=True,mode="json")],"index":chunk.content_index,})elifchunk.type=="response.created":response_metadata["id"]=chunk.response.idelifchunk.type=="response.completed":msg=cast(AIMessage,(_construct_lc_result_from_responses_api(chunk.response,schema=schema).generations[0].message),)ifparsed:=msg.additional_kwargs.get("parsed"):additional_kwargs["parsed"]=parsedifreasoning:=msg.additional_kwargs.get("reasoning"):additional_kwargs["reasoning"]=reasoningusage_metadata=msg.usage_metadataresponse_metadata={k:vfork,vinmsg.response_metadata.items()ifk!="id"}elifchunk.type=="response.output_item.added"andchunk.item.type=="message":id=chunk.item.idelif(chunk.type=="response.output_item.added"andchunk.item.type=="function_call"):tool_call_chunks.append({"type":"tool_call_chunk","name":chunk.item.name,"args":chunk.item.arguments,"id":chunk.item.call_id,"index":chunk.output_index,})additional_kwargs[_FUNCTION_CALL_IDS_MAP_KEY]={chunk.item.call_id:chunk.item.id}elifchunk.type=="response.output_item.done"andchunk.item.typein("web_search_call","file_search_call","computer_call",):additional_kwargs["tool_outputs"]=[chunk.item.model_dump(exclude_none=True,mode="json")]elifchunk.type=="response.function_call_arguments.delta":tool_call_chunks.append({"type":"tool_call_chunk","args":chunk.delta,"index":chunk.output_index,})elifchunk.type=="response.refusal.done":additional_kwargs["refusal"]=chunk.refusalelse:returnNonereturnChatGenerationChunk(message=AIMessageChunk(content=content,# type: ignore[arg-type]tool_call_chunks=tool_call_chunks,usage_metadata=usage_metadata,response_metadata=response_metadata,additional_kwargs=additional_kwargs,id=id,))