[Python] Add web_search_preview tool to langchain invoke

Discussão em 'Python' iniciado por Stack, Junho 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working with LangGraph and LangChain, and I need to add the web_search_preview tool to my model when using model.invoke() within a state graph node. I can see that the tool works when used directly with the OpenAI client, but I'm having trouble integrating it into my LangGraph workflow.

    Current Implementation


    Here's my current code structure:

    from langchain_openai import ChatOpenAI
    from langchain_core.messages import SystemMessage, HumanMessage
    from langgraph.graph import StateGraph, START, END
    from langgraph.checkpoint.memory import MemorySaver

    # Model initialization
    model = ChatOpenAI(
    model="gpt-4.1",
    temperature=0.3
    )

    # State definition
    class AssistanceState(TypedDict):
    messages: Annotated[list, add_messages]
    user_profile: Dict
    chat_id: str
    # ... other fields

    # Node function where I want to use web_search_preview
    def conversation(state: AssistanceState) -> AssistanceState:
    user_profile = state.get("user_profile", {})

    system_content = f"""
    Eres un asistente culinario experto...
    {user_profile}
    """

    messages = [SystemMessage(content=system_content)] + state["messages"]
    config = {"configurable": {"thread_id": state["chat_id"]}}

    # This is where I want to add web_search_preview tool
    response = model.invoke(messages, config=config)

    return {"messages": [response]}

    What I've Tried


    I've attempted to bind the tool to the model like this:

    # Attempt 1: Binding tools before invoke
    model.bind_tools(tools=[{"type": "web_search_preview"}])
    response = model.invoke(messages, config=config, tools=[{"type": "web_search_preview"}])

    # Attempt 2: Passing tools directly in invoke
    response = model.invoke(messages, config=config, tools=[{"type": "web_search_preview"}])

    # Attempt 3: Using the OpenAI client directly (this works)
    def search_web_openai(query: str) -> str:
    response = client.responses.create(
    model="gpt-4.1",
    tools=[{"type": "web_search_preview"}],
    input=query
    )
    # ... process response

    The Problem


    The web_search_preview tool works perfectly when I use the OpenAI client directly, but when I try to integrate it into my LangGraph workflow using model.invoke(), it doesn't seem to be available or the model doesn't use it.

    Questions

    1. How can I properly add the web_search_preview tool to a LangChain model within a LangGraph node?
    2. Is there a specific way to configure the model to use this tool in the context of a state graph?
    3. Are there any additional configurations needed for the tool to work with LangChain's invoke method?
    Environment

    • LangChain: Latest version
    • LangGraph: Latest version
    • OpenAI API: Using GPT-4.1
    • Python: 3.9+
    Expected Behavior


    I want the model to be able to use the web_search_preview tool when responding to user queries within my LangGraph workflow, similar to how it works when using the OpenAI client directly.

    Any help or guidance would be greatly appreciated!

    Continue reading...

Compartilhe esta Página