Skip to content

supervisor agent can not work in output_mode="last_message" with return_direct tools #185

Open
@sjy3

Description

@sjy3

my env :
'''
Python 3.13.3
langchain 0.3.25
langgraph 0.4.3
'''

I get the following issues when supervisor agent work in output_mode="last_message" with sub agent bind return direct tools.

ValueError: {'code': None, 'param': 'messages.[4].role', 'message': "Invalid parameter: messages with role 'tool' must be a response to a preceeding message with 'tool_calls'.", 'type': 'invalid_request_error'}

And the code example as follows:

from langchain.tools import tool
from langgraph.prebuilt import create_react_agent
from langchain_openai import ChatOpenAI
from langgraph_supervisor import create_supervisor


@tool("multiply-tool", return_direct=True)
def multiply(a: float, b: float) -> float:
    """Multiply two numbers"""
    print(f"{a}*{b} input add")
    return a * b

product_cost_agent = create_react_agent(
    model=ChatOpenAI(
        model="gpt-4o",
        temperature=0.0,
    ),
    tools=[multiply],
    prompt=(
        "You are a price calculation agent.\n\n"
        "Responsible for providing the prices required to purchase multiple products."
    ),
    name="product_cost_agent",
)


@tool("multiply-tool", return_direct=True)
def sku_search(product_id: str) -> dict:
    """Get a product info by sku_id"""
    return {
        "product_id": product_id,
        "product_name": "test_product",
        "price": 12.8,
    }


product_info_agent = create_react_agent(
    model=ChatOpenAI(
        model="gpt-4o",
        temperature=0.0,
    ),
    tools=[sku_search],
    prompt=(
        "You are a product information query agent.\n\n"
        "responsible for providing product information based on the product ID input."
    ),
    name="product_info_agent",
)

supervisor_agent = create_supervisor(
    model=ChatOpenAI(
        model="gpt-4o",
        temperature=0.0,
    ),
    agents=[product_info_agent, product_cost_agent],
    prompt=(
        "You are a supervisory agent managing two agents:\n"
        "- product_info_agent. Please assign the task of querying product information to it\n"
        "- product_cost_agent. Please assign the task of calculating product costs to it\n"
        "You don't need to personally handle any work, just summarize the final results and return them."
    ),
    add_handoff_back_messages=True,
    # output_mode="full_history",
    output_mode="last_message",
).compile(
    name="supervisor_agent",
    # checkpointer=checkpointer,
    # store=store
)

if __name__ == '__main__':
    for chunk in supervisor_agent.stream(
            {"messages": [("human", "How much does it cost to purchase 10 pieces of a product with product_i 11111?")]},
            stream_mode="values",
            debug=True,
    ):
        chunk["messages"][-1].pretty_print()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions