Skip to content

Using OpenAIConversationsSession causes a bug. #1882

@alexliusyx

Description

@alexliusyx

"""
Example demonstrating session memory functionality.

This example shows how to use session memory to maintain conversation history
across multiple agent runs without manually handling .to_input_list().
"""

import asyncio
from agents import Agent, OpenAIConversationsSession, Runner

async def main():
# Create an agent
agent = Agent(
name="Assistant",
instructions="Reply very concisely.",
)

# Create a session instance that will persist across runs
session = OpenAIConversationsSession()

print("=== Session Example ===")
print("The agent will remember previous messages automatically.\n")

# First turn
print("First turn:")
print("User: What city is the Golden Gate Bridge in?")
result = await Runner.run(
    agent,
    "What city is the Golden Gate Bridge in?",
    session=session,
)
print(f"Assistant: {result.final_output}")
print()

# Second turn - the agent will remember the previous conversation
print("Second turn:")
print("User: What state is it in?")
result = await Runner.run(agent, "What state is it in?", session=session)
print(f"Assistant: {result.final_output}")
print()

# Third turn - continuing the conversation
print("Third turn:")
print("User: What's the population of that state?")
result = await Runner.run(
    agent,
    "What's the population of that state?",
    session=session,
)
print(f"Assistant: {result.final_output}")
print()

print("=== Conversation Complete ===")
print("Notice how the agent remembered the context from previous turns!")
print("Sessions automatically handles conversation history.")

# Demonstrate the limit parameter - get only the latest 2 items
print("\n=== Latest Items Demo ===")
latest_items = await session.get_items(limit=2)
# print(latest_items)
print("Latest 2 items:")
for i, msg in enumerate(latest_items, 1):
    role = msg.get("role", "unknown")
    content = msg.get("content", "")
    print(f"  {i}. {role}: {content}")

print(f"\nFetched {len(latest_items)} out of total conversation history.")

# Get all items to show the difference
all_items = await session.get_items()
# print(all_items)
print(f"Total items in session: {len(all_items)}")

if name == "main":
asyncio.run(main())

=== Session Example ===
The agent will remember previous messages automatically.

First turn:
User: What city is the Golden Gate Bridge in?
Assistant: The Golden Gate Bridge is in San Francisco, California — it spans the Golden Gate strait to Marin County.

Second turn:
User: What state is it in?
Error getting response: Error code: 400 - {'error': {'message': "Unknown parameter: 'input[1].status'.", 'type': 'invalid_request_error', 'param': 'input[1].status', 'code': 'unknown_parameter'}}. (request_id: req_f7c6cd1538a44413953ff3dfdde12d5b)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions