When using SQLiteSession with Runner.run() in the OpenAI Agent SDK, tools are not invoked on every call if the same input is repeated. Instead, the second, fourth, sixth (even-numbered) calls return a cached/remembered answer directly without calling the tool.
If I remove the session parameter from Runner.run(), the tool is invoked every time as expected.
Code to Reproduce
from agents import Agent, Runner, SQLiteSession, function_tool
session = SQLiteSession("conversation_123")
@function_tool
async def find_prime_minister(name: str) -> str:
"""
Find the current Prime Minister of a given country.
Args:
name (str): The name of the country.
Returns:
str: A sentence stating the current Prime Minister of the specified country.
"""
print(f"++++++++Finding Prime Minister for {name}++++++++")
if name.lower() == "canada":
return "Prime Minister of Canada is Justin Trudeau"
elif name.lower() == "uk":
return "Prime Minister of UK is Rishi Sunak"
elif name.lower() == "pakistan":
return "Prime Minister of Pakistan is Mian Shahbaz Sharif"
else:
return "Prime Minister not found for this country."
agent = Agent(
name="Assistant",
instructions="Reply very concisely.",
tools=[find_prime_minister],
tool_use_behavior="stop_on_first_tool",
)
# First call
result = await Runner.run(
agent,
"Who is the current Prime Minister of Canada?",
session=session
)
print(result.final_output)
# Second call with the same input
result = await Runner.run(
agent,
"Who is the current Prime Minister of Canada?",
session=session
)
print(result.final_output)
Expected Behavior
The tool find_prime_minister should be invoked on every call, regardless of session state, so that:
++++++++Finding Prime Minister for Canada++++++++
Prime Minister of Canada is Justin Trudeau
is printed on every run.
Actual Behavior
- First call: Works as expected (tool is invoked)
- Second call: Tool is not called; answer is returned directly from somewhere in the session history.
- Third call: Works again
- Fourth call: Skips tool call again
This alternates (tool called on odd calls, skipped on even calls).
Notes
Removing session=session from Runner.run() makes the tool run every time. This suggests that the session memory mechanism is returning a previous answer without re-triggering the tool, possibly as a caching or reasoning optimization. However, for tools with side effects or real-time data, skipping the tool can lead to incorrect or outdated results.