16 Commits

Author SHA1 Message Date
Kazuhiro Sera b4faf7090c feat(core): add model call timeouts (#4428) 2026-08-16 11:15:36 +09:00
Kazuhiro Sera 0c60a196af feat(models): preserve raw usage payloads (#4279) 2026-08-07 19:24:39 +09:00
Kazuhiro Sera f78df37ee9 feat: consistently accept typed objects and dictionaries for SDK configuration (#3917) 2026-07-24 08:04:45 +09:00
Kazuhiro Sera df2ddd41eb feat: support GPT-5.6 request controls (#3794) 2026-07-11 06:48:04 +09:00
Adrian f6ba91b120 Runtime handling updates (#3451)
## Summary
- Refresh runtime handling around session and tool-call flows.
- Adjust model configuration metadata used by runtime integrations.
- Add focused coverage for the updated behavior.

## Validation
- .venv/bin/python -m pytest tests/model_settings/test_serialization.py
tests/models/test_trace_config.py
tests/mcp/test_streamable_http_client_factory.py
tests/test_run_context_approvals.py
tests/test_run_state.py::TestRunState::test_trace_api_key_serialization_is_opt_in
tests/realtime/test_session.py
- .venv/bin/ruff check <touched files>
- .venv/bin/ruff format --check <touched files>
- git diff --check
2026-05-18 16:53:20 -07:00
Kazuhiro Sera 574a598fae feat: add context management model setting (#3128) 2026-05-05 20:55:16 +09:00
Kazuhiro Sera 3a5267340a feat: add opt-in model retry policies (#2651) 2026-03-12 12:32:54 +09:00
Wen-Tien Chang 48164ecac2 Add prompt_cache_retention to ModelSettings (#2095) 2025-11-18 14:22:08 +09:00
Kazuhiro Sera e3b4856a20 Fix #1407 Add reasoning.effort="minimal" and "verbosity" params to ModelSettings (#1439)
This pull request resolves #1407 ; the "minimal" reasoning effort param
is already supported.
2025-08-13 10:00:13 -04:00
Zain Memon 5b758bd81a Add logprobs to ModelSettings (#971) 2025-08-11 13:59:44 +09:00
Kazuhiro Sera eafd8df998 Fix #968 by upgrading openai package to the latest (#1034)
This pull request resolves #968
2025-07-12 10:59:47 +09:00
Stephan Fitzpatrick 6b94ad0f85 Add Sessions for Automatic Conversation History Management (#752)
# Overview

Resolves #745

This PR introduces **Sessions**, a new core feature that automatically
maintains conversation history across multiple agent runs, eliminating
the need to manually handle `.to_input_list()` between turns.

## Key Features

### 🧠 Automatic Memory Management
- **Zero-effort conversation continuity**: Agents automatically remember
previous context without manual state management
- **Session-based organization**: Each conversation is isolated by
unique session IDs
- **Seamless integration**: Works with existing `Runner.run()`,
`Runner.run_sync()`, and `Runner.run_streamed()` methods

### 🔌 Extensible Session Protocol
- **Library-agnostic design**: Clean protocol interface allows any
storage backend
- **Drop-in implementations**: Easy integration with Redis, PostgreSQL,
MongoDB, or any custom storage
- **Production-ready interface**: Async-first design with proper error
handling and type safety
- **Vendor flexibility**: Library authors can provide their own Session
implementations

### 💾 Built-in SQLite Implementation
- **In-memory SQLite**: Perfect for temporary conversations during
development
- **Persistent SQLite**: File-based storage for conversations that
survive application restarts
- **Thread-safe operations**: Production-ready with connection pooling
and proper concurrency handling

### 🔧 Simple API
```python
# Before: Manual conversation management
result1 = await Runner.run(agent, "What's the weather?")
new_input = result1.to_input_list() + [{"role": "user", "content": "How about tomorrow?"}]
result2 = await Runner.run(agent, new_input)

# After: Automatic with Sessions
session = SQLiteSession("user_123")

result1 = await Runner.run(agent, "What's the weather?", session=session)
result2 = await Runner.run(agent, "How about tomorrow?", session=session)  # Remembers context automatically
```

## What's Included

### Core Session Protocol
- **`Session` Protocol**: Clean, async interface that any storage
backend can implement
- **Type-safe design**: Full type hints and runtime validation
- **Standard operations**: `get_items()`, `add_items()`, `pop_item()`,
`clear_session()`
- **Extensibility-first**: Designed for third-party implementations

### Reference Implementation
- **`SQLiteSession` Class**: Production-ready SQLite implementation
- **Automatic schema management**: Creates tables and indexes
automatically
- **Connection pooling**: Thread-safe operations with proper resource
management
- **Flexible storage**: In-memory or persistent file-based databases

### Runner Integration
- **New `session` parameter**: Drop-in addition to existing `Runner`
methods
- **Backward compatibility**: Zero breaking changes to existing code
- **Automatic history management**: Prepends conversation history before
each run

## Session Protocol for Library Authors

The Session protocol provides a clean interface for implementing custom
storage backends:

```python
from agents.memory import Session
from typing import List

class MyCustomSession:
    """Custom session implementation following the Session protocol."""

    def __init__(self, session_id: str):
        self.session_id = session_id
        # Your initialization here

    async def get_items(self, limit: int | None = None) -> List[dict]:
        """Retrieve conversation history for this session."""
        # Your implementation here
        pass

    async def add_items(self, items: List[dict]) -> None:
        """Store new items for this session."""
        # Your implementation here
        pass

    async def pop_item(self) -> dict | None:
        """Remove and return the most recent item from this session."""
        # Your implementation here
        pass

    async def clear_session(self) -> None:
        """Clear all items for this session."""
        # Your implementation here
        pass

# Works seamlessly with any custom implementation
result = await Runner.run(agent, "Hello", session=MyCustomSession("session_123"))
```

### Example Third-Party Implementations

```python
# Redis-based session (hypothetical library implementation)
from redis_sessions import RedisSession
session = RedisSession("user_123", redis_url="redis://localhost:6379")

# PostgreSQL-based session (hypothetical library implementation) 
from postgres_sessions import PostgreSQLSession
session = PostgreSQLSession("user_123", connection_string="postgresql://...")

# Cloud-based session (hypothetical library implementation)
from cloud_sessions import CloudSession
session = CloudSession("user_123", api_key="...", region="us-east-1")

# All work identically with the Runner
result = await Runner.run(agent, "Hello", session=session)
```

## Benefits

### For Application Developers
- **Reduces boilerplate**: No more manual `.to_input_list()` management
- **Prevents memory leaks**: Automatic cleanup and organized storage
- **Easier debugging**: Clear conversation history tracking
- **Flexible storage**: Choose the right backend for your needs

### For Library Authors
- **Clean integration**: Simple protocol to implement for any storage
backend
- **Type safety**: Full type hints and runtime validation
- **Async-first**: Modern async/await design throughout
- **Documentation**: Comprehensive examples and API reference

### For Applications
- **Better user experience**: Seamless conversation continuity
- **Scalable architecture**: Support for multiple concurrent
conversations
- **Flexible deployment**: In-memory for development, production storage
for scale
- **Multi-agent support**: Same conversation history can be shared
across different agents

## Usage Examples

### Basic Usage with SQLiteSession
```python
from agents import Agent, Runner, SQLiteSession

agent = Agent(name="Assistant", instructions="Reply concisely.")
session = SQLiteSession("conversation_123")

# Conversation flows naturally
await Runner.run(agent, "Hi, I'm planning a trip to Japan", session=session)
await Runner.run(agent, "What's the best time to visit?", session=session)
await Runner.run(agent, "How about cherry blossom season?", session=session)
```

### Multiple Sessions with Isolation
```python
# Different users get separate conversation histories
session_alice = SQLiteSession("user_alice")
session_bob = SQLiteSession("user_bob")

# Completely isolated conversations
await Runner.run(agent, "I like pizza", session=session_alice)
await Runner.run(agent, "I like sushi", session=session_bob)
```

### Persistent vs In-Memory Storage
```python
# In-memory database (lost when process ends)
session = SQLiteSession("user_123")

# Persistent file-based database
session = SQLiteSession("user_123", "conversations.db")
```

### Session Management Operations
```python
session = SQLiteSession("user_123")

# Get all items in a session
items = await session.get_items()

# Add new items to a session
new_items = [
    {"role": "user", "content": "Hello"},
    {"role": "assistant", "content": "Hi there!"}
]
await session.add_items(new_items)

# Remove and return the most recent item (useful for corrections)
last_item = await session.pop_item()

# Clear all items from a session
await session.clear_session()
```

### Message Correction Pattern
```python
# User wants to correct their last question
user_message = await session.pop_item()  # Remove user's question
assistant_message = await session.pop_item()  # Remove agent's response

# Ask a corrected question
result = await Runner.run(
    agent,
    "What's 2 + 3?",  # Corrected question
    session=session
)
```

## Technical Details

### Session Protocol Design
- **Async-first**: All operations are async for non-blocking I/O
- **Type-safe**: Full type hints with runtime validation
- **Error handling**: Graceful degradation and detailed error messages
- **Resource management**: Proper cleanup and connection handling

### SQLiteSession Implementation
- **Thread-safe operations** with connection pooling
- **Automatic schema management** with proper indexing
- **JSON serialization** for message storage
- **Memory-efficient** conversation retrieval and storage
- **Cross-platform compatibility**

## Breaking Changes

None. This is a purely additive feature that doesn't affect existing
functionality.

## Documentation

- Updated core concepts in `docs/index.md` to highlight Sessions as a
key primitive
- New comprehensive guide at `docs/sessions.md` with protocol
implementation examples
- Enhanced `docs/running_agents.md` with automatic vs manual
conversation management
- Full API reference integration via `docs/ref/memory.md`
- Implementation guide for library authors

Sessions represent a significant architectural improvement for building
conversational AI applications with the Agents SDK. The extensible
Session protocol enables the ecosystem to provide specialized storage
backends while maintaining a consistent, simple API for application
developers.

---------

Co-authored-by: Rohan Mehta <rm@openai.com>
2025-07-10 12:18:49 -04:00
tconley1428 017ad69de1 Annotating the openai.Omit type so that ModelSettings can be serialized by pydantic (#938)
Because `openai.Omit` is not a type which can be serialized by
`pydantic`, it produces difficulty in consistently
serializing/deserializing types across the agents SDK, many of which are
`pydantic` types. This adds an annotation to enable `pydantic` to
serialize `Omit`, in particular in the case of `ModelSettings` which
contains `Omit` in its `extra_headers`
2025-06-27 11:25:06 -04:00
Niv Hertz d88bf14360 Bugfix | Fixed a bug when calling reasoning models with store=False (#920)
resolves https://github.com/openai/openai-agents-python/issues/919
2025-06-24 11:14:19 -04:00
Rohan Mehta 0eee6b8305 Allow arbitrary kwargs in model (#842)
Sometimes users want to provide parameters specific to a model provider.
This is an escape hatch.
2025-06-10 18:14:34 -04:00
Rohan Mehta 3755ea8658 Create to_json_dict for ModelSettings (#582)
Now that `ModelSettings` has `Reasoning`, a non-primitive object,
`dataclasses.as_dict()` wont work. It will raise an error when you try
to serialize (e.g. for tracing). This ensures the object is actually
serializable.
2025-04-23 20:39:07 -04:00