Skip to content

fix the invalid paraments error in last_message model with return direct type tools #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sjy3
Copy link

@sjy3 sjy3 commented May 21, 2025

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

This PR fix the following issues when supervisor agent work in output_mode="last_message" with sub agent with 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()

@sjy3
Copy link
Author

sjy3 commented May 29, 2025

Fix #185

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant