
    h              
           d dl Z d dlmZ d dlmZmZ d dlmZ dedef   dede	e
eef      f   fdZd	edee   f   dedee	e
eef         f   fd
ZdededefdZy)    N)	Awaitable)AnyCallable)toolfunc.returnc                 T     dt         dt        t        t         t        f      f fd}|S )Nxr   c                     d |       dgS Ncustom_tool_call_output)typeoutput )r
   r   s    `/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/langchain_openai/tools/custom_tool.pywrappedz#_make_wrapped_func.<locals>.wrapped	   s    2d1gFGG    )strlistdictr   )r   r   s   ` r   _make_wrapped_funcr      s-    H3 H4S#X/ H Nr   	coroutinec           	      `     dt         dt         dt        t        t        t         f      f fd}|S )Nargskwargsr   c                  <   K    | i | d {   }d|dgS 7 
wr   r   )r   r   resultr   s      r   r   z(_make_wrapped_coroutine.<locals>.wrapped   s,      $1&112fEFF 2s   )r   r   r   r   )r   r   s   ` r   _make_wrapped_coroutiner      s5    GS GC GDc3h4H G Nr   r   r   c                  x    dt         dt        f   dt        ffd}| rt        | d         rs || d         S |S )aj  Decorator to create an OpenAI custom tool.

    Custom tools allow for tools with (potentially long) freeform string inputs.

    See below for an example using LangGraph:

    .. code-block:: python

        @custom_tool
        def execute_code(code: str) -> str:
            """Execute python code."""
            return "27"


        llm = ChatOpenAI(model="gpt-5", output_version="responses/v1")

        agent = create_react_agent(llm, [execute_code])

        input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
        for step in agent.stream(
            {"messages": [input_message]},
            stream_mode="values",
        ):
            step["messages"][-1].pretty_print()

    You can also specify a format for a corresponding context-free grammar using the
    ``format`` kwarg:

    .. code-block:: python

        from langchain_openai import ChatOpenAI, custom_tool
        from langgraph.prebuilt import create_react_agent

        grammar = """
        start: expr
        expr: term (SP ADD SP term)* -> add
        | term
        term: factor (SP MUL SP factor)* -> mul
        | factor
        factor: INT
        SP: " "
        ADD: "+"
        MUL: "*"
        %import common.INT
        """

        format = {"type": "grammar", "syntax": "lark", "definition": grammar}

        # highlight-next-line
        @custom_tool(format=format)
        def do_math(input_string: str) -> str:
            """Do a mathematical operation."""
            return "27"


        llm = ChatOpenAI(model="gpt-5", output_version="responses/v1")

        agent = create_react_agent(llm, [do_math])

        input_message = {"role": "user", "content": "Use the tool to calculate 3^3."}
        for step in agent.stream(
            {"messages": [input_message]},
            stream_mode="values",
        ):
            step["messages"][-1].pretty_print()
    r   .r   c                    ddi}dv rj                  d      |d<    t        dddi|       }||_        | j                  |_        t        j                  |       rt        |       |_        |S t        |       |_
        |S )Nr   custom_toolformatinfer_schemaFr   )popr   metadata__doc__descriptioninspectiscoroutinefunctionr   r   r   r   )r   r%   tool_objr   s      r   	decoratorzcustom_tool.<locals>.decorator]   s    M*v!'H!5HX545U5f5d;$#||&&t,!8!>H  /t4HMr   r   )r   r   callable)r   r   r+   s    ` r   r!   r!      sG    Hc* s  a!&a!!r   )r(   collections.abcr   typingr   r   langchain_core.toolsr   r   r   r   r   r   r!   r   r   r   <module>r0      s     %   %Xc3h/ HS$tCQTH~BV=V4W Ys^+,c9T$sCx.1223Ts Tc Tc Tr   