
    $h              
           S SK r S SKJr  S SKJrJr  S SKJr  S\S\4   S\S\	\
\\4      4   4S jrS	\S\\   4   S\S\\	\
\\4         4   4S
 jrS\S\S\4S jrg)    N)	Awaitable)AnyCallable)toolfunc.returnc                 X   ^  S[         S[        [        [         [        4      4U 4S jjnU$ )Nxr   c                    > ST" U 5      S./$ Ncustom_tool_call_output)typeoutput )r
   r   s    Z/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_openai/tools/custom_tool.pywrapped#_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   ^  S[         S[         S[        [        [        [         4      4U 4S jjnU$ )Nargskwargsr   c                  <   >#    T" U 0 UD6I S h  vN nSUS./$  N
7fr   r   )r   r   resultr   s      r   r   (_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                     ^ S[         S[        4   S[        4U4S jjnU (       a%  [        U S   5      (       a  T(       d  U" U S   5      $ U$ )a  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                   > SS0nST;   a  TR                  S5      US'   [        SSS0TD6" U 5      nXl        U R                  Ul        [
        R                  " U 5      (       a  [        U 5      Ul        U$ [        U 5      Ul
        U$ )Nr   custom_toolformatinfer_schemaFr   )popr   metadata__doc__descriptioninspectiscoroutinefunctionr    r   r   r   )r   r'   tool_objr   s      r   	decoratorcustom_tool.<locals>.decorator]   s    M*v!'H!5HX5U5f5d;$#||&&t,,!8!>H  /t4HMr   r   )r   r   callable)r   r   r-   s    ` r   r#   r#      sJ    Hc* s  a!!&a!!r   )r*   collections.abcr   typingr   r   langchain_core.toolsr   r   r   r   r   r    r#   r   r   r   <module>r3      s     %   %Xc3h/ HS$tCQTH~BV=V4W Ys^+,c9T$sCx.1223Ts Tc Tc Tr   