
    3fi                    v   U d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
mZmZmZmZ ddlZddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZ ddlmZ ddlmZm Z m!Z!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'  ejP                  e)      Z* ede      Z+e,e-ef   e.e+   z  e.z  Z/de0d<   e,e+z  Z1de0d<   ddZ2 G d de&      Z3y)zAzure OpenAI chat wrapper.    )annotationsN)AsyncIterator	AwaitableCallableIterator)AnyLiteral	TypeAliasTypeVar)LanguageModelInput)LangSmithParams)ChatGenerationChunk
ChatResult)Runnable)from_envsecret_from_env)is_basemodel_subclass)	BaseModelField	SecretStrmodel_validator)Self)BaseChatOpenAI_get_default_model_profile_BM)boundr
   _DictOrPydanticClass_DictOrPydanticc                <    t        | t              xr t        |       S N)
isinstancetyper   )objs    `/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_openai/chat_models/azure.py_is_pydantic_classr%      s    c4 ?%:3%??    c                  4    e Zd ZU dZ e edd            Zded<   	  edd	      Zded
<   	  ed edd            Z	ded<   	  ed e
ddgd            Zded<   	  e e
dd            Zded<   	 dZded<   	 dZded<   	 dZded<   	  e edd            Zded<   	 d Zd!ed"<   	  edd#	      Zded$<   	  ed      Zd%ed&<   	  edd'	      Zd(ed)<   	 ed?d*       Zed@d+       ZedAd,       Z ed-.      dBd/       Z ed-.      dBd0       ZedC fd1       ZedDd2       ZedCd3       ZedC fd4       Z 	 dE	 	 	 	 	 dF fd5Z!	 dE	 	 	 	 	 dG fd6Z"dd7	 	 	 	 	 	 	 dH fd8Z#dI fd9Z$	 	 	 	 	 	 dJ fd:Z%	 dEd;d<dd=	 	 	 	 	 	 	 	 	 	 	 dK fd>Z& xZ'S )LAzureChatOpenAIuM6  Azure OpenAI chat model integration.

    Setup:
        Head to the Azure [OpenAI quickstart guide](https://learn.microsoft.com/en-us/azure/ai-foundry/openai/chatgpt-quickstart?tabs=keyless%2Ctypescript-keyless%2Cpython-new%2Ccommand-line&pivots=programming-language-python)
        to create your Azure OpenAI deployment.

        Then install `langchain-openai` and set environment variables
        `AZURE_OPENAI_API_KEY` and `AZURE_OPENAI_ENDPOINT`:

        ```bash
        pip install -U langchain-openai

        export AZURE_OPENAI_API_KEY="your-api-key"
        export AZURE_OPENAI_ENDPOINT="https://your-endpoint.openai.azure.com/"
        ```

    Key init args — completion params:
        azure_deployment:
            Name of Azure OpenAI deployment to use.
        temperature:
            Sampling temperature.
        max_tokens:
            Max number of tokens to generate.
        logprobs:
            Whether to return logprobs.

    Key init args — client params:
        api_version:
            Azure OpenAI REST API version to use (distinct from the version of the
            underlying model). [See more on the different versions.](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#rest-api-versioning)
        timeout:
            Timeout for requests.
        max_retries:
            Max number of retries.
        organization:
            OpenAI organization ID. If not passed in will be read from env
            var `OPENAI_ORG_ID`.
        model:
            The name of the underlying OpenAI model. Used for tracing and token
            counting. Does not affect completion. E.g. `'gpt-4'`, `'gpt-35-turbo'`, etc.
        model_version:
            The version of the underlying OpenAI model. Used for tracing and token
            counting. Does not affect completion. E.g., `'0125'`, `'0125-preview'`, etc.

    See full list of supported init args and their descriptions in the params section.

    Instantiate:
        ```python
        from langchain_openai import AzureChatOpenAI

        model = AzureChatOpenAI(
            azure_deployment="your-deployment",
            api_version="2024-05-01-preview",
            temperature=0,
            max_tokens=None,
            timeout=None,
            max_retries=2,
            # organization="...",
            # model="gpt-35-turbo",
            # model_version="0125",
            # other params...
        )
        ```

    !!! note
        Any param which is not explicitly supported will be passed directly to the
        `openai.AzureOpenAI.chat.completions.create(...)` API every time to the model is
        invoked.

        For example:

        ```python
        from langchain_openai import AzureChatOpenAI
        import openai

        AzureChatOpenAI(..., logprobs=True).invoke(...)

        # results in underlying API call of:

        openai.AzureOpenAI(..).chat.completions.create(..., logprobs=True)

        # which is also equivalent to:

        AzureChatOpenAI(...).invoke(..., logprobs=True)
        ```

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

        ```python
        AIMessage(
            content="J'adore programmer.",
            usage_metadata={
                "input_tokens": 28,
                "output_tokens": 6,
                "total_tokens": 34,
            },
            response_metadata={
                "token_usage": {
                    "completion_tokens": 6,
                    "prompt_tokens": 28,
                    "total_tokens": 34,
                },
                "model_name": "gpt-4",
                "system_fingerprint": "fp_7ec89fabc6",
                "prompt_filter_results": [
                    {
                        "prompt_index": 0,
                        "content_filter_results": {
                            "hate": {"filtered": False, "severity": "safe"},
                            "self_harm": {"filtered": False, "severity": "safe"},
                            "sexual": {"filtered": False, "severity": "safe"},
                            "violence": {"filtered": False, "severity": "safe"},
                        },
                    }
                ],
                "finish_reason": "stop",
                "logprobs": None,
                "content_filter_results": {
                    "hate": {"filtered": False, "severity": "safe"},
                    "self_harm": {"filtered": False, "severity": "safe"},
                    "sexual": {"filtered": False, "severity": "safe"},
                    "violence": {"filtered": False, "severity": "safe"},
                },
            },
            id="run-6d7a5282-0de0-4f27-9cc0-82a9db9a3ce9-0",
        )
        ```

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

        ```python
        AIMessageChunk(content="", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content="J", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content="'", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content="ad", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content="ore", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content=" la", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(
            content=" programm", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f"
        )
        AIMessageChunk(content="ation", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(content=".", id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f")
        AIMessageChunk(
            content="",
            response_metadata={
                "finish_reason": "stop",
                "model_name": "gpt-4",
                "system_fingerprint": "fp_811936bd4f",
            },
            id="run-a6f294d3-0700-4f6a-abc2-c6ef1178c37f",
        )
        ```

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

        ```python
        AIMessageChunk(
            content="J'adore la programmation.",
            response_metadata={
                "finish_reason": "stop",
                "model_name": "gpt-4",
                "system_fingerprint": "fp_811936bd4f",
            },
            id="run-ba60e41c-9258-44b8-8f3a-2f10599643b3",
        )
        ```

    Async:
        ```python
        await model.ainvoke(messages)

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

        # batch:
        # await model.abatch([messages])
        ```

    Tool calling:
        ```python
        from pydantic import BaseModel, Field


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

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


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

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


        model_with_tools = model.bind_tools([GetWeather, GetPopulation])
        ai_msg = model_with_tools.invoke(
            "Which city is hotter today and which is bigger: LA or NY?"
        )
        ai_msg.tool_calls
        ```

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

    Structured output:
        ```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: int | None = Field(
                description="How funny the joke is, from 1 to 10"
            )


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

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

        See `AzureChatOpenAI.with_structured_output()` for more.

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

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

    Image input:
        ```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 = model.invoke([message])
        ai_msg.content
        ```

        ```python
        "The weather in the image appears to be quite pleasant. The sky is mostly clear"
        ```

    Token usage:
        ```python
        ai_msg = model.invoke(messages)
        ai_msg.usage_metadata
        ```

        ```python
        {"input_tokens": 28, "output_tokens": 5, "total_tokens": 33}
        ```
    Logprobs:
        ```python
        logprobs_model = model.bind(logprobs=True)
        ai_msg = logprobs_model.invoke(messages)
        ai_msg.response_metadata["logprobs"]
        ```

        ```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": [],
                },
            ]
        }
        ```

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

        ```python
        {
            "token_usage": {
                "completion_tokens": 6,
                "prompt_tokens": 28,
                "total_tokens": 34,
            },
            "model_name": "gpt-35-turbo",
            "system_fingerprint": None,
            "prompt_filter_results": [
                {
                    "prompt_index": 0,
                    "content_filter_results": {
                        "hate": {"filtered": False, "severity": "safe"},
                        "self_harm": {"filtered": False, "severity": "safe"},
                        "sexual": {"filtered": False, "severity": "safe"},
                        "violence": {"filtered": False, "severity": "safe"},
                    },
                }
            ],
            "finish_reason": "stop",
            "logprobs": None,
            "content_filter_results": {
                "hate": {"filtered": False, "severity": "safe"},
                "self_harm": {"filtered": False, "severity": "safe"},
                "sexual": {"filtered": False, "severity": "safe"},
                "violence": {"filtered": False, "severity": "safe"},
            },
        }
        ```
    AZURE_OPENAI_ENDPOINTN)default)default_factoryz
str | Noneazure_endpointazure_deployment)r*   aliasdeployment_nameapi_versionOPENAI_API_VERSION)r.   r+   openai_api_versionapi_keyAZURE_OPENAI_API_KEYOPENAI_API_KEYzSecretStr | Noneopenai_api_keyAZURE_OPENAI_AD_TOKENazure_ad_tokenzCallable[[], str] | Noneazure_ad_token_providerz#Callable[[], Awaitable[str]] | Noneazure_ad_async_token_provider strmodel_versionOPENAI_API_TYPEazureopenai_api_typeTboolvalidate_base_urlmodel
model_namezdict[str, Any] | Nonedisabled_paramsmax_completion_tokensz
int | None
max_tokensc                
    g dS )zGet the namespace of the LangChain object.

        Returns:
            `["langchain", "chat_models", "azure_openai"]`
        )	langchainchat_modelsazure_openai clss    r$   get_lc_namespacez AzureChatOpenAI.get_lc_namespace=  s
     <;r&   c                    dddS )z0Get the mapping of secret environment variables.r4   r7   )r6   r8   rL   selfs    r$   
lc_secretszAzureChatOpenAI.lc_secretsF  s     55
 	
r&   c                     y)z0Check if the class is serializable in langchain.TrL   rM   s    r$   is_lc_serializablez"AzureChatOpenAI.is_lc_serializableN  s     r&   after)modec                     j                    j                   dk  rd}t        |       j                   ( j                   dkD  r j                  rd}t        |       j                  % j                  r j                  dk(  rn	ddi _         j
                  xs, t        j                  d      xs t        j                  d       _        t         fd	d
D              rd _	         j                  }|r6 j                  r*d|vrd}t        |       j                  rd}t        |       j                   j                   j                   j                  r j                  j!                         nd j"                  r j"                  j!                         nd j$                   j
                   j                   j&                  i  j(                  xs i ddi j*                  d} j,                   j,                  |d<    j.                  sOd j0                  i}t3        j4                  di || _         j6                  j8                  j:                   _         j<                  sjd j>                  i} j@                  r j@                  |d<   t3        jB                  di || _"         jD                  j8                  j:                   _         S )z?Validate that api key and python package exists in environment.N   zn must be at least 1.zn must be 1 when streaming.zgpt-4oparallel_tool_callsOPENAI_ORG_IDOPENAI_ORGANIZATIONc              3  <   K   | ]  }t        |d       d u   y wr    )getattr).0keyrR   s     r$   	<genexpr>z7AzureChatOpenAI.validate_environment.<locals>.<genexpr>l  s'      
 D#t$,
s   )
stream_usageopenai_proxyopenai_api_basebase_urlclientroot_clientasync_clientroot_async_clienthttp_clienthttp_async_clientTz/openaizAs of openai>=1.0.0, Azure endpoints should be specified via the `azure_endpoint` param not `openai_api_base` (or alias `base_url`).a  As of openai>=1.0.0, if `azure_deployment` (or alias `deployment_name`) is specified then `base_url` (or alias `openai_api_base`) should not be. If specifying `azure_deployment`/`deployment_name` then use `azure_endpoint` instead of `base_url`.

For example, you could specify:

azure_endpoint="https://xxx.openai.azure.com/", azure_deployment="my-deployment"

Or you can equivalently specify:

base_url="https://xxx.openai.azure.com/openai/deployments/my-deployment"z
User-Agentz%langchain-partner-python-azure-openai)r0   r,   r-   r3   r8   r9   organizationre   timeoutdefault_headersdefault_querymax_retriesrj   r9   rL   )#n
ValueError	streamingrE   rD   openai_organizationosgetenvallrb   rd   rB   r/   r2   r,   r6   get_secret_valuer8   r9   request_timeoutrn   ro   rp   rf   rj   openaiAzureOpenAIrg   chatcompletionsrh   rk   r:   AsyncAzureOpenAIri   )rR   msgrd   client_paramssync_specificasync_specifics   `     r$   validate_environmentz$AzureChatOpenAI.validate_environmentS  s    66$&&1*)CS/!66$&&1*/CS/!'4??h#>(=t'D$ $$ 0yy)0yy./ 	   

 
 !%D ..t55/- 
 !o%##	_  !o%22"11 $ 4 4:>:M:M##446SW ;?:M:M##446SW'+'C'C 44,,++ ''-2 E  "//%
( '+/+;+;M-({{*D,<,<=M%11SMS]SD**//;;DK  +T-C-CDN1166 78 &,%<%< && &D" !% 6 6 ; ; G GDr&   c                j    | j                   &| j                  t        | j                        | _         | S )z$Set model profile if not overridden.)profiler/   r   rQ   s    r$   _set_model_profilez"AzureChatOpenAI._set_model_profile  s0     <<D$8$8$D5d6J6JKDLr&   c                4    d| j                   it        |   S )zGet the identifying parameters.r-   )r/   super_identifying_params)rR   	__class__s    r$   r   z#AzureChatOpenAI._identifying_params  s'      4 4
g)
 	
r&   c                     y)Nzazure-openai-chatrL   rQ   s    r$   	_llm_typezAzureChatOpenAI._llm_type  s    "r&   c                4    | j                   | j                  dS )z'Get the attributes relevant to tracing.r@   r2   r   rQ   s    r$   lc_attributeszAzureChatOpenAI.lc_attributes  s       $33"&"9"9
 	
r&   c                L    t         |   }d|v r|j                  d      |d<   |S )z8Get the default parameters for calling Azure OpenAI API.rG   rF   )r   _default_paramspop)rR   paramsr   s     r$   r   zAzureChatOpenAI._default_params  s0     (6!.4jj.FF*+r&   c                L   t        |   dd|i|}d|d<   | j                  re| j                  rH| j                  | j                  vr0| j                  dz   | j                  j	                  d      z   |d<   |S | j                  |d<   |S | j
                  r| j
                  |d<   |S )z,Get the parameters used to invoke the model.stopr?   ls_provider-ls_model_namerL   )r   _get_ls_paramsrD   r=   lstripr/   )rR   r   kwargsr   r   s       r$   r   zAzureChatOpenAI._get_ls_params  s     '<T<V< '}??!!d&8&8&OOOc)D,>,>,E,Ec,JJ '  +///'  !!&*&:&:F?#r&   c                X   t         	|   ||      }t        |t              s|j	                         }|d   D ]$  }|j                  dd       dk(  sd}t        |       d|v rF|d   }| j                  r| d| j                   }|j                  xs i |_        ||j                  d<   d|v r'|j                  xs i |_        |d   |j                  d<   t        |j                  |d   d	
      D ]9  \  }}|j                  xs i |_        |j                  di       |j                  d<   ; |S )Nchoicesfinish_reasoncontent_filterzKAzure has not provided the response due to a content filter being triggeredrC   r   rD   prompt_filter_resultsF)strictcontent_filter_results)r   _create_chat_resultr!   dict
model_dumpgetrr   r=   
llm_outputzipgenerationsgeneration_info)
rR   responser   chat_resultresr   rC   chat_genresponse_choicer   s
            r$   r   z#AzureChatOpenAI._create_chat_result  sc   
 g1(OL(D)**,HI& 	&Cww-1AA&  !o%	& hW%E!! '4#5#5"67%0%;%;%ArK"38K""<0"h.%0%;%;%ArK">F'?K""#:; *-##Xi%8*
 	%Ho (0'?'?'E2H$APATAT("BH$$%=>		 r&   )r   c                   t        |   |fd|i|}| j                  |      r,|j                  d      s| j                  r| j                  |d<   |S )zGGet the request payload, using deployment name for Azure Responses API.r   rC   )r   _get_request_payload_use_responses_apir   r/   )rR   input_r   r   payloadr   s        r$   r   z$AzureChatOpenAI._get_request_payload  sX     '.vKDKFK ##G,KK($$#33GGr&   c                ~    | j                  i || j                        rt        |   |i |S t        |   |i |S )+Route to Chat Completions or Responses API.)r   model_kwargsr   _stream_responses_stream)rR   argsr   r   s      r$   r   zAzureChatOpenAI._stream0  sK    ""#Bf#B0A0A#BC7,d=f==w///r&   c                  K   | j                  i || j                        rt        |   |i |2 3 d{   }| t        |   |i |2 3 d{   }| 7 (6 y7 6 yw)r   N)r   r   r   _astream_responses_astream)rR   r   r   chunkr   s       r$   r   zAzureChatOpenAI._astream6  s      ""#Bf#B0A0A#BC$w94J6J  e$w/@@  e	J@sI   /A'A!AA!A'A%A#A%A'A!!A'#A%%A'json_schemaFmethodinclude_rawr   c               ,    t        |   |f|||d|S )aX8  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,
                - A Pydantic class,
                - Or 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 `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](https://platform.openai.com/docs/guides/structured-outputs/json-mode).
                    Note that if using JSON mode then you must include instructions for
                    formatting the output into the desired schema into the model call

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

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

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

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

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

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

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

                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 are passed through to the model.

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

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

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

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

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

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

            Support for `tools` added.

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

            Pass `kwargs` through to the model.

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

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

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

            ```python
            from typing import Optional

            from langchain_openai import AzureChatOpenAI
            from pydantic import BaseModel, Field


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

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


            model = AzureChatOpenAI(
                azure_deployment="...", model="gpt-4o", temperature=0
            )
            structured_model = model.with_structured_output(AnswerWithJustification)

            structured_model.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.'
            # )
            ```

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

            ```python
            from typing import Optional

            from langchain_openai import AzureChatOpenAI
            from pydantic import BaseModel, Field


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

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


            model = AzureChatOpenAI(
                azure_deployment="...", model="gpt-4o", temperature=0
            )
            structured_model = model.with_structured_output(
                AnswerWithJustification, method="function_calling"
            )

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

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

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

            ```python
            from langchain_openai import AzureChatOpenAI
            from pydantic import BaseModel


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

                answer: str
                justification: str


            model = AzureChatOpenAI(
                azure_deployment="...", model="gpt-4o", temperature=0
            )
            structured_model = model.with_structured_output(
                AnswerWithJustification, include_raw=True
            )

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

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

            ```python
            from typing_extensions import Annotated, TypedDict

            from langchain_openai import AzureChatOpenAI


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

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


            model = AzureChatOpenAI(
                azure_deployment="...", model="gpt-4o", temperature=0
            )
            structured_model = model.with_structured_output(AnswerWithJustification)

            structured_model.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.'
            # }
            ```

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

            ```python
            from langchain_openai import AzureChatOpenAI

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

                model = AzureChatOpenAI(
                    azure_deployment="...",
                    model="gpt-4o",
                    temperature=0,
                )
                structured_model = model.with_structured_output(oai_schema)

                structured_model.invoke(
                    "What weighs more a pound of bricks or a pound of feathers"
                )
                # -> {
                #     'answer': 'They weigh the same',
                #     'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The weight is the same, but the volume and density of the two substances differ.'
                # }
            ```

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

            ```python
            from langchain_openai import AzureChatOpenAI
            from pydantic import BaseModel


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


            model = AzureChatOpenAI(
                azure_deployment="...",
                model="gpt-4o",
                temperature=0,
            )
            structured_model = model.with_structured_output(
                AnswerWithJustification, method="json_mode", include_raw=True
            )

            structured_model.invoke(
                "Answer the following question. "
                "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
                "What's heavier a pound of bricks or a pound of feathers?"
            )
            # -> {
            #     'raw': AIMessage(content='{\\n    "answer": "They are both the same weight.",\\n    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'),
            #     'parsed': AnswerWithJustification(answer='They are both the same weight.', justification='Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'),
            #     'parsing_error': None
            # }
            ```

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

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

            structured_model.invoke(
                "Answer the following question. "
                "Make sure to return a JSON blob with keys 'answer' and 'justification'.\\n\\n"
                "What's heavier a pound of bricks or a pound of feathers?"
            )
            # -> {
            #     'raw': AIMessage(content='{\\n    "answer": "They are both the same weight.",\\n    "justification": "Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight." \\n}'),
            #     'parsed': {
            #         'answer': 'They are both the same weight.',
            #         'justification': 'Both a pound of bricks and a pound of feathers weigh one pound. The difference lies in the volume and density of the materials, not the weight.'
            #     },
            #     'parsing_error': None
            # }
            ```

        r   )r   with_structured_output)rR   schemar   r   r   r   r   s         r$   r   z&AzureChatOpenAI.with_structured_outputA  s/    R
 w-
!{6
MS
 	
r&   )returnz	list[str])r   zdict[str, str])r   rA   )r   r   )r   zdict[str, Any])r   r<   r    )r   list[str] | Noner   r   r   r   )r   zdict | openai.BaseModelr   zdict | Noner   r   )r   r   r   r   r   r   r   r   )r   r   r   r   r   zIterator[ChatGenerationChunk])r   r   r   r   r   z"AsyncIterator[ChatGenerationChunk])r   z_DictOrPydanticClass | Noner   z7Literal['function_calling', 'json_mode', 'json_schema']r   rA   r   zbool | Noner   r   r   z-Runnable[LanguageModelInput, _DictOrPydantic])(__name__
__module____qualname____doc__r   r   r,   __annotations__r/   r2   r   r6   r8   r9   r:   r=   r@   rB   rD   rE   rG   classmethodrO   propertyrS   rU   r   r   r   r   r   r   r   r   r   r   r   r   r   __classcell__)r   s   @r$   r(   r(   "   s1   hT "' !8$G"NJ  #(<N"OOZO &+ !5tD&
  T (-'#%56
(N$  V','(?N(N$  9=5< JN!#FM
 M3
 #( !2GD#OZ  ."t" #4w?J
? .34-@O*@* #47NOJ
O/< < 
 
   '"j #jX '" # 
 
 # # 
 
   (,$7:	( (,%)% %% 
	%V "&	" 	
  
(0		$'		+	 /3K
 KX!"K
+K
 H	K

 K
 K
 K
 
7K
 K
r&   r(   )r#   r   r   rA   )4r   
__future__r   loggingru   collections.abcr   r   r   r   typingr   r	   r
   r   rz   langchain_core.language_modelsr   *langchain_core.language_models.chat_modelsr   langchain_core.outputsr   r   langchain_core.runnablesr   langchain_core.utilsr   r   langchain_core.utils.pydanticr   pydanticr   r   r   r   typing_extensionsr   !langchain_openai.chat_models.baser   r   	getLoggerr   loggerr   r   r<   r"   r   r   r   r%   r(   rL   r&   r$   <module>r      s      "  	 H H 3 3  = F B - : ? A A " X			8	$ e9%"&sCx.49"<t"C i C!CZ '@j
n j
r&   