Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions test-langchain2/main_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain.agents import create_agent
from langchain_core.prompts import ChatPromptTemplate

import sentry_sdk
Expand All @@ -27,35 +27,36 @@ def calculate_tip(bill_amount: float, tip_percentage: float) -> float:
def my_pipeline(llm, tools):
with sentry_sdk.start_transaction(name="langchain-sync-tool"):
# Create agent with tools
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant that can use tools to help answer questions."),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent = create_agent(
model=llm,
tools=tools,
system_prompt="You are a helpful assistant that can use tools to help answer questions",
).with_config(config={"run_name": "MyAgent1"})

# Test with weather tool
result1 = agent_executor.invoke({
"input": "What's the weather like in San Francisco, CA?"
})
result1 = agent.invoke(
{
"messages": [
{
"role": "user",
"content": "What's the weather like in San Francisco, CA?",
}
]
}
)
print("Weather Result:")
print(result1)

# Test with calculation tool
result2 = agent_executor.invoke({
"input": "Calculate a 18% tip on a $45.50 bill"
})
result2 = agent.invoke({"input": "Calculate a 18% tip on a $45.50 bill"})
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: create_agent expects {"messages": ...} input, but test-langchain2/main_tool.py:51 and 57-59 use {"input": ...}, causing a KeyError.
Severity: CRITICAL | Confidence: 0.95

🔍 Detailed Analysis

The create_agent function in LangChain v1 expects input in a {"messages": [{"role": "user", "content": "..."}]} format. At test-langchain2/main_tool.py:51 and test-langchain2/main_tool.py:57-59, the agent is invoked with an outdated {"input": "..."} format. This mismatch will cause the agent to not find the expected messages key, leading to a KeyError or ValidationError during execution.

💡 Suggested Fix

Update the input format at test-langchain2/main_tool.py:51 and test-langchain2/main_tool.py:57-59 from {"input": "..."} to {"messages": [{"role": "user", "content": "..."}]} to match LangChain v1's create_agent expectations.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: test-langchain2/main_tool.py#L51

Potential issue: The `create_agent` function in LangChain v1 expects input in a
`{"messages": [{"role": "user", "content": "..."}]}` format. At
`test-langchain2/main_tool.py:51` and `test-langchain2/main_tool.py:57-59`, the agent is
invoked with an outdated `{"input": "..."}` format. This mismatch will cause the agent
to not find the expected `messages` key, leading to a `KeyError` or `ValidationError`
during execution.

Did we get this right? 👍 / 👎 to inform future reviews.

print("Tip Calculation Result:")
print(result2)

# Test streaming with agent
print("Streaming Result:")
for chunk in agent_executor.stream({
"input": "What's the weather in New York and calculate a 20% tip on $30?"
}):
for chunk in agent.stream(
{"input": "What's the weather in New York and calculate a 20% tip on $30?"}
):
print(chunk)


Expand Down
6 changes: 3 additions & 3 deletions test-langchain2/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ requires-python = ">=3.12"

dependencies = [
"ipdb>=0.13.13",
"langchain>=0.3.27",
"langchain-community>=0.3.27",
"langchain-openai>=0.3.28",
"langchain>=1.0.3",
# "langchain-community>=0.3.27",
"langchain-openai>=1.0.2",
"pudb>=2025.1",
"sentry-sdk",
"tiktoken>=0.10.0",
Expand Down
Empty file modified test-langchain2/run_tool.sh
100644 → 100755
Empty file.
Loading