openai-agents 0.2.8__py3-none-any.whl → 0.2.10__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of openai-agents might be problematic. Click here for more details.
- agents/__init__.py +3 -1
- agents/_run_impl.py +44 -7
- agents/agent.py +36 -4
- agents/extensions/memory/__init__.py +15 -0
- agents/extensions/memory/sqlalchemy_session.py +312 -0
- agents/extensions/models/litellm_model.py +11 -6
- agents/extensions/models/litellm_provider.py +3 -1
- agents/function_schema.py +2 -2
- agents/handoffs.py +3 -3
- agents/lifecycle.py +40 -1
- agents/mcp/server.py +59 -8
- agents/memory/__init__.py +9 -2
- agents/memory/openai_conversations_session.py +94 -0
- agents/memory/session.py +0 -270
- agents/memory/sqlite_session.py +275 -0
- agents/model_settings.py +8 -3
- agents/models/__init__.py +13 -0
- agents/models/chatcmpl_converter.py +5 -0
- agents/models/chatcmpl_stream_handler.py +81 -17
- agents/models/default_models.py +58 -0
- agents/models/interface.py +4 -0
- agents/models/openai_chatcompletions.py +4 -2
- agents/models/openai_provider.py +3 -1
- agents/models/openai_responses.py +24 -10
- agents/realtime/config.py +3 -0
- agents/realtime/events.py +11 -0
- agents/realtime/model_events.py +10 -0
- agents/realtime/openai_realtime.py +39 -5
- agents/realtime/session.py +7 -0
- agents/repl.py +7 -3
- agents/run.py +132 -7
- agents/tool.py +9 -1
- agents/tracing/processors.py +2 -2
- {openai_agents-0.2.8.dist-info → openai_agents-0.2.10.dist-info}/METADATA +16 -14
- {openai_agents-0.2.8.dist-info → openai_agents-0.2.10.dist-info}/RECORD +37 -32
- {openai_agents-0.2.8.dist-info → openai_agents-0.2.10.dist-info}/WHEEL +0 -0
- {openai_agents-0.2.8.dist-info → openai_agents-0.2.10.dist-info}/licenses/LICENSE +0 -0
agents/__init__.py
CHANGED
|
@@ -46,7 +46,7 @@ from .items import (
|
|
|
46
46
|
TResponseInputItem,
|
|
47
47
|
)
|
|
48
48
|
from .lifecycle import AgentHooks, RunHooks
|
|
49
|
-
from .memory import Session, SQLiteSession
|
|
49
|
+
from .memory import OpenAIConversationsSession, Session, SessionABC, SQLiteSession
|
|
50
50
|
from .model_settings import ModelSettings
|
|
51
51
|
from .models.interface import Model, ModelProvider, ModelTracing
|
|
52
52
|
from .models.multi_provider import MultiProvider
|
|
@@ -221,7 +221,9 @@ __all__ = [
|
|
|
221
221
|
"RunHooks",
|
|
222
222
|
"AgentHooks",
|
|
223
223
|
"Session",
|
|
224
|
+
"SessionABC",
|
|
224
225
|
"SQLiteSession",
|
|
226
|
+
"OpenAIConversationsSession",
|
|
225
227
|
"RunContextWrapper",
|
|
226
228
|
"TContext",
|
|
227
229
|
"RunErrorDetails",
|
agents/_run_impl.py
CHANGED
|
@@ -509,13 +509,29 @@ class RunImpl:
|
|
|
509
509
|
# Regular function tool call
|
|
510
510
|
else:
|
|
511
511
|
if output.name not in function_map:
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
512
|
+
if output_schema is not None and output.name == "json_tool_call":
|
|
513
|
+
# LiteLLM could generate non-existent tool calls for structured outputs
|
|
514
|
+
items.append(ToolCallItem(raw_item=output, agent=agent))
|
|
515
|
+
functions.append(
|
|
516
|
+
ToolRunFunction(
|
|
517
|
+
tool_call=output,
|
|
518
|
+
# this tool does not exist in function_map, so generate ad-hoc one,
|
|
519
|
+
# which just parses the input if it's a string, and returns the
|
|
520
|
+
# value otherwise
|
|
521
|
+
function_tool=_build_litellm_json_tool_call(output),
|
|
522
|
+
)
|
|
516
523
|
)
|
|
517
|
-
|
|
518
|
-
|
|
524
|
+
continue
|
|
525
|
+
else:
|
|
526
|
+
_error_tracing.attach_error_to_current_span(
|
|
527
|
+
SpanError(
|
|
528
|
+
message="Tool not found",
|
|
529
|
+
data={"tool_name": output.name},
|
|
530
|
+
)
|
|
531
|
+
)
|
|
532
|
+
error = f"Tool {output.name} not found in agent {agent.name}"
|
|
533
|
+
raise ModelBehaviorError(error)
|
|
534
|
+
|
|
519
535
|
items.append(ToolCallItem(raw_item=output, agent=agent))
|
|
520
536
|
functions.append(
|
|
521
537
|
ToolRunFunction(
|
|
@@ -961,7 +977,10 @@ class RunImpl:
|
|
|
961
977
|
context_wrapper: RunContextWrapper[TContext],
|
|
962
978
|
config: RunConfig,
|
|
963
979
|
) -> ToolsToFinalOutputResult:
|
|
964
|
-
"""
|
|
980
|
+
"""Determine if tool results should produce a final output.
|
|
981
|
+
Returns:
|
|
982
|
+
ToolsToFinalOutputResult: Indicates whether final output is ready, and the output value.
|
|
983
|
+
"""
|
|
965
984
|
if not tool_results:
|
|
966
985
|
return _NOT_FINAL_OUTPUT
|
|
967
986
|
|
|
@@ -1190,3 +1209,21 @@ class LocalShellAction:
|
|
|
1190
1209
|
# "id": "out" + call.tool_call.id, # TODO remove this, it should be optional
|
|
1191
1210
|
},
|
|
1192
1211
|
)
|
|
1212
|
+
|
|
1213
|
+
|
|
1214
|
+
def _build_litellm_json_tool_call(output: ResponseFunctionToolCall) -> FunctionTool:
|
|
1215
|
+
async def on_invoke_tool(_ctx: ToolContext[Any], value: Any) -> Any:
|
|
1216
|
+
if isinstance(value, str):
|
|
1217
|
+
import json
|
|
1218
|
+
|
|
1219
|
+
return json.loads(value)
|
|
1220
|
+
return value
|
|
1221
|
+
|
|
1222
|
+
return FunctionTool(
|
|
1223
|
+
name=output.name,
|
|
1224
|
+
description=output.name,
|
|
1225
|
+
params_json_schema={},
|
|
1226
|
+
on_invoke_tool=on_invoke_tool,
|
|
1227
|
+
strict_json_schema=True,
|
|
1228
|
+
is_enabled=True,
|
|
1229
|
+
)
|
agents/agent.py
CHANGED
|
@@ -17,6 +17,11 @@ from .items import ItemHelpers
|
|
|
17
17
|
from .logger import logger
|
|
18
18
|
from .mcp import MCPUtil
|
|
19
19
|
from .model_settings import ModelSettings
|
|
20
|
+
from .models.default_models import (
|
|
21
|
+
get_default_model_settings,
|
|
22
|
+
gpt_5_reasoning_settings_required,
|
|
23
|
+
is_gpt_5_default,
|
|
24
|
+
)
|
|
20
25
|
from .models.interface import Model
|
|
21
26
|
from .prompts import DynamicPromptFunction, Prompt, PromptUtil
|
|
22
27
|
from .run_context import RunContextWrapper, TContext
|
|
@@ -168,10 +173,10 @@ class Agent(AgentBase, Generic[TContext]):
|
|
|
168
173
|
"""The model implementation to use when invoking the LLM.
|
|
169
174
|
|
|
170
175
|
By default, if not set, the agent will use the default model configured in
|
|
171
|
-
`
|
|
176
|
+
`agents.models.get_default_model()` (currently "gpt-4.1").
|
|
172
177
|
"""
|
|
173
178
|
|
|
174
|
-
model_settings: ModelSettings = field(default_factory=
|
|
179
|
+
model_settings: ModelSettings = field(default_factory=get_default_model_settings)
|
|
175
180
|
"""Configures model-specific tuning parameters (e.g. temperature, top_p).
|
|
176
181
|
"""
|
|
177
182
|
|
|
@@ -205,8 +210,9 @@ class Agent(AgentBase, Generic[TContext]):
|
|
|
205
210
|
This lets you configure how tool use is handled.
|
|
206
211
|
- "run_llm_again": The default behavior. Tools are run, and then the LLM receives the results
|
|
207
212
|
and gets to respond.
|
|
208
|
-
- "stop_on_first_tool": The output
|
|
209
|
-
|
|
213
|
+
- "stop_on_first_tool": The output from the first tool call is treated as the final result.
|
|
214
|
+
In other words, it isn’t sent back to the LLM for further processing but is used directly
|
|
215
|
+
as the final output.
|
|
210
216
|
- A StopAtTools object: The agent will stop running if any of the tools listed in
|
|
211
217
|
`stop_at_tool_names` is called.
|
|
212
218
|
The final output will be the output of the first matching tool call.
|
|
@@ -285,6 +291,26 @@ class Agent(AgentBase, Generic[TContext]):
|
|
|
285
291
|
f"got {type(self.model_settings).__name__}"
|
|
286
292
|
)
|
|
287
293
|
|
|
294
|
+
if (
|
|
295
|
+
# The user sets a non-default model
|
|
296
|
+
self.model is not None
|
|
297
|
+
and (
|
|
298
|
+
# The default model is gpt-5
|
|
299
|
+
is_gpt_5_default() is True
|
|
300
|
+
# However, the specified model is not a gpt-5 model
|
|
301
|
+
and (
|
|
302
|
+
isinstance(self.model, str) is False
|
|
303
|
+
or gpt_5_reasoning_settings_required(self.model) is False # type: ignore
|
|
304
|
+
)
|
|
305
|
+
# The model settings are not customized for the specified model
|
|
306
|
+
and self.model_settings == get_default_model_settings()
|
|
307
|
+
)
|
|
308
|
+
):
|
|
309
|
+
# In this scenario, we should use a generic model settings
|
|
310
|
+
# because non-gpt-5 models are not compatible with the default gpt-5 model settings.
|
|
311
|
+
# This is a best-effort attempt to make the agent work with non-gpt-5 models.
|
|
312
|
+
self.model_settings = ModelSettings()
|
|
313
|
+
|
|
288
314
|
if not isinstance(self.input_guardrails, list):
|
|
289
315
|
raise TypeError(
|
|
290
316
|
f"Agent input_guardrails must be a list, got {type(self.input_guardrails).__name__}"
|
|
@@ -356,6 +382,8 @@ class Agent(AgentBase, Generic[TContext]):
|
|
|
356
382
|
tool_name: str | None,
|
|
357
383
|
tool_description: str | None,
|
|
358
384
|
custom_output_extractor: Callable[[RunResult], Awaitable[str]] | None = None,
|
|
385
|
+
is_enabled: bool
|
|
386
|
+
| Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]] = True,
|
|
359
387
|
) -> Tool:
|
|
360
388
|
"""Transform this agent into a tool, callable by other agents.
|
|
361
389
|
|
|
@@ -371,11 +399,15 @@ class Agent(AgentBase, Generic[TContext]):
|
|
|
371
399
|
when to use it.
|
|
372
400
|
custom_output_extractor: A function that extracts the output from the agent. If not
|
|
373
401
|
provided, the last message from the agent will be used.
|
|
402
|
+
is_enabled: Whether the tool is enabled. Can be a bool or a callable that takes the run
|
|
403
|
+
context and agent and returns whether the tool is enabled. Disabled tools are hidden
|
|
404
|
+
from the LLM at runtime.
|
|
374
405
|
"""
|
|
375
406
|
|
|
376
407
|
@function_tool(
|
|
377
408
|
name_override=tool_name or _transforms.transform_string_function_style(self.name),
|
|
378
409
|
description_override=tool_description or "",
|
|
410
|
+
is_enabled=is_enabled,
|
|
379
411
|
)
|
|
380
412
|
async def run_agent(context: RunContextWrapper, input: str) -> str:
|
|
381
413
|
from .run import Runner
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
"""Session memory backends living in the extensions namespace.
|
|
3
|
+
|
|
4
|
+
This package contains optional, production-grade session implementations that
|
|
5
|
+
introduce extra third-party dependencies (database drivers, ORMs, etc.). They
|
|
6
|
+
conform to the :class:`agents.memory.session.Session` protocol so they can be
|
|
7
|
+
used as a drop-in replacement for :class:`agents.memory.session.SQLiteSession`.
|
|
8
|
+
"""
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from .sqlalchemy_session import SQLAlchemySession # noqa: F401
|
|
12
|
+
|
|
13
|
+
__all__: list[str] = [
|
|
14
|
+
"SQLAlchemySession",
|
|
15
|
+
]
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
"""SQLAlchemy-powered Session backend.
|
|
2
|
+
|
|
3
|
+
Usage::
|
|
4
|
+
|
|
5
|
+
from agents.extensions.memory import SQLAlchemySession
|
|
6
|
+
|
|
7
|
+
# Create from SQLAlchemy URL (uses asyncpg driver under the hood for Postgres)
|
|
8
|
+
session = SQLAlchemySession.from_url(
|
|
9
|
+
session_id="user-123",
|
|
10
|
+
url="postgresql+asyncpg://app:secret@db.example.com/agents",
|
|
11
|
+
create_tables=True, # If you want to auto-create tables, set to True.
|
|
12
|
+
)
|
|
13
|
+
|
|
14
|
+
# Or pass an existing AsyncEngine that your application already manages
|
|
15
|
+
session = SQLAlchemySession(
|
|
16
|
+
session_id="user-123",
|
|
17
|
+
engine=my_async_engine,
|
|
18
|
+
create_tables=True, # If you want to auto-create tables, set to True.
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
await Runner.run(agent, "Hello", session=session)
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
from __future__ import annotations
|
|
25
|
+
|
|
26
|
+
import asyncio
|
|
27
|
+
import json
|
|
28
|
+
from typing import Any
|
|
29
|
+
|
|
30
|
+
from sqlalchemy import (
|
|
31
|
+
TIMESTAMP,
|
|
32
|
+
Column,
|
|
33
|
+
ForeignKey,
|
|
34
|
+
Index,
|
|
35
|
+
Integer,
|
|
36
|
+
MetaData,
|
|
37
|
+
String,
|
|
38
|
+
Table,
|
|
39
|
+
Text,
|
|
40
|
+
delete,
|
|
41
|
+
insert,
|
|
42
|
+
select,
|
|
43
|
+
text as sql_text,
|
|
44
|
+
update,
|
|
45
|
+
)
|
|
46
|
+
from sqlalchemy.ext.asyncio import AsyncEngine, async_sessionmaker, create_async_engine
|
|
47
|
+
|
|
48
|
+
from ...items import TResponseInputItem
|
|
49
|
+
from ...memory.session import SessionABC
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class SQLAlchemySession(SessionABC):
|
|
53
|
+
"""SQLAlchemy implementation of :pyclass:`agents.memory.session.Session`."""
|
|
54
|
+
|
|
55
|
+
_metadata: MetaData
|
|
56
|
+
_sessions: Table
|
|
57
|
+
_messages: Table
|
|
58
|
+
|
|
59
|
+
def __init__(
|
|
60
|
+
self,
|
|
61
|
+
session_id: str,
|
|
62
|
+
*,
|
|
63
|
+
engine: AsyncEngine,
|
|
64
|
+
create_tables: bool = False,
|
|
65
|
+
sessions_table: str = "agent_sessions",
|
|
66
|
+
messages_table: str = "agent_messages",
|
|
67
|
+
):
|
|
68
|
+
"""Initializes a new SQLAlchemySession.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
session_id (str): Unique identifier for the conversation.
|
|
72
|
+
engine (AsyncEngine): A pre-configured SQLAlchemy async engine. The engine
|
|
73
|
+
must be created with an async driver (e.g., 'postgresql+asyncpg://',
|
|
74
|
+
'mysql+aiomysql://', or 'sqlite+aiosqlite://').
|
|
75
|
+
create_tables (bool, optional): Whether to automatically create the required
|
|
76
|
+
tables and indexes. Defaults to False for production use. Set to True for
|
|
77
|
+
development and testing when migrations aren't used.
|
|
78
|
+
sessions_table (str, optional): Override the default table name for sessions if needed.
|
|
79
|
+
messages_table (str, optional): Override the default table name for messages if needed.
|
|
80
|
+
"""
|
|
81
|
+
self.session_id = session_id
|
|
82
|
+
self._engine = engine
|
|
83
|
+
self._lock = asyncio.Lock()
|
|
84
|
+
|
|
85
|
+
self._metadata = MetaData()
|
|
86
|
+
self._sessions = Table(
|
|
87
|
+
sessions_table,
|
|
88
|
+
self._metadata,
|
|
89
|
+
Column("session_id", String, primary_key=True),
|
|
90
|
+
Column(
|
|
91
|
+
"created_at",
|
|
92
|
+
TIMESTAMP(timezone=False),
|
|
93
|
+
server_default=sql_text("CURRENT_TIMESTAMP"),
|
|
94
|
+
nullable=False,
|
|
95
|
+
),
|
|
96
|
+
Column(
|
|
97
|
+
"updated_at",
|
|
98
|
+
TIMESTAMP(timezone=False),
|
|
99
|
+
server_default=sql_text("CURRENT_TIMESTAMP"),
|
|
100
|
+
onupdate=sql_text("CURRENT_TIMESTAMP"),
|
|
101
|
+
nullable=False,
|
|
102
|
+
),
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
self._messages = Table(
|
|
106
|
+
messages_table,
|
|
107
|
+
self._metadata,
|
|
108
|
+
Column("id", Integer, primary_key=True, autoincrement=True),
|
|
109
|
+
Column(
|
|
110
|
+
"session_id",
|
|
111
|
+
String,
|
|
112
|
+
ForeignKey(f"{sessions_table}.session_id", ondelete="CASCADE"),
|
|
113
|
+
nullable=False,
|
|
114
|
+
),
|
|
115
|
+
Column("message_data", Text, nullable=False),
|
|
116
|
+
Column(
|
|
117
|
+
"created_at",
|
|
118
|
+
TIMESTAMP(timezone=False),
|
|
119
|
+
server_default=sql_text("CURRENT_TIMESTAMP"),
|
|
120
|
+
nullable=False,
|
|
121
|
+
),
|
|
122
|
+
Index(
|
|
123
|
+
f"idx_{messages_table}_session_time",
|
|
124
|
+
"session_id",
|
|
125
|
+
"created_at",
|
|
126
|
+
),
|
|
127
|
+
sqlite_autoincrement=True,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
# Async session factory
|
|
131
|
+
self._session_factory = async_sessionmaker(self._engine, expire_on_commit=False)
|
|
132
|
+
|
|
133
|
+
self._create_tables = create_tables
|
|
134
|
+
|
|
135
|
+
# ---------------------------------------------------------------------
|
|
136
|
+
# Convenience constructors
|
|
137
|
+
# ---------------------------------------------------------------------
|
|
138
|
+
@classmethod
|
|
139
|
+
def from_url(
|
|
140
|
+
cls,
|
|
141
|
+
session_id: str,
|
|
142
|
+
*,
|
|
143
|
+
url: str,
|
|
144
|
+
engine_kwargs: dict[str, Any] | None = None,
|
|
145
|
+
**kwargs: Any,
|
|
146
|
+
) -> SQLAlchemySession:
|
|
147
|
+
"""Create a session from a database URL string.
|
|
148
|
+
|
|
149
|
+
Args:
|
|
150
|
+
session_id (str): Conversation ID.
|
|
151
|
+
url (str): Any SQLAlchemy async URL, e.g. "postgresql+asyncpg://user:pass@host/db".
|
|
152
|
+
engine_kwargs (dict[str, Any] | None): Additional keyword arguments forwarded to
|
|
153
|
+
sqlalchemy.ext.asyncio.create_async_engine.
|
|
154
|
+
**kwargs: Additional keyword arguments forwarded to the main constructor
|
|
155
|
+
(e.g., create_tables, custom table names, etc.).
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
SQLAlchemySession: An instance of SQLAlchemySession connected to the specified database.
|
|
159
|
+
"""
|
|
160
|
+
engine_kwargs = engine_kwargs or {}
|
|
161
|
+
engine = create_async_engine(url, **engine_kwargs)
|
|
162
|
+
return cls(session_id, engine=engine, **kwargs)
|
|
163
|
+
|
|
164
|
+
async def _serialize_item(self, item: TResponseInputItem) -> str:
|
|
165
|
+
"""Serialize an item to JSON string. Can be overridden by subclasses."""
|
|
166
|
+
return json.dumps(item, separators=(",", ":"))
|
|
167
|
+
|
|
168
|
+
async def _deserialize_item(self, item: str) -> TResponseInputItem:
|
|
169
|
+
"""Deserialize a JSON string to an item. Can be overridden by subclasses."""
|
|
170
|
+
return json.loads(item) # type: ignore[no-any-return]
|
|
171
|
+
|
|
172
|
+
# ------------------------------------------------------------------
|
|
173
|
+
# Session protocol implementation
|
|
174
|
+
# ------------------------------------------------------------------
|
|
175
|
+
async def _ensure_tables(self) -> None:
|
|
176
|
+
"""Ensure tables are created before any database operations."""
|
|
177
|
+
if self._create_tables:
|
|
178
|
+
async with self._engine.begin() as conn:
|
|
179
|
+
await conn.run_sync(self._metadata.create_all)
|
|
180
|
+
self._create_tables = False # Only create once
|
|
181
|
+
|
|
182
|
+
async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:
|
|
183
|
+
"""Retrieve the conversation history for this session.
|
|
184
|
+
|
|
185
|
+
Args:
|
|
186
|
+
limit: Maximum number of items to retrieve. If None, retrieves all items.
|
|
187
|
+
When specified, returns the latest N items in chronological order.
|
|
188
|
+
|
|
189
|
+
Returns:
|
|
190
|
+
List of input items representing the conversation history
|
|
191
|
+
"""
|
|
192
|
+
await self._ensure_tables()
|
|
193
|
+
async with self._session_factory() as sess:
|
|
194
|
+
if limit is None:
|
|
195
|
+
stmt = (
|
|
196
|
+
select(self._messages.c.message_data)
|
|
197
|
+
.where(self._messages.c.session_id == self.session_id)
|
|
198
|
+
.order_by(self._messages.c.created_at.asc())
|
|
199
|
+
)
|
|
200
|
+
else:
|
|
201
|
+
stmt = (
|
|
202
|
+
select(self._messages.c.message_data)
|
|
203
|
+
.where(self._messages.c.session_id == self.session_id)
|
|
204
|
+
# Use DESC + LIMIT to get the latest N
|
|
205
|
+
# then reverse later for chronological order.
|
|
206
|
+
.order_by(self._messages.c.created_at.desc())
|
|
207
|
+
.limit(limit)
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
result = await sess.execute(stmt)
|
|
211
|
+
rows: list[str] = [row[0] for row in result.all()]
|
|
212
|
+
|
|
213
|
+
if limit is not None:
|
|
214
|
+
rows.reverse()
|
|
215
|
+
|
|
216
|
+
items: list[TResponseInputItem] = []
|
|
217
|
+
for raw in rows:
|
|
218
|
+
try:
|
|
219
|
+
items.append(await self._deserialize_item(raw))
|
|
220
|
+
except json.JSONDecodeError:
|
|
221
|
+
# Skip corrupted rows
|
|
222
|
+
continue
|
|
223
|
+
return items
|
|
224
|
+
|
|
225
|
+
async def add_items(self, items: list[TResponseInputItem]) -> None:
|
|
226
|
+
"""Add new items to the conversation history.
|
|
227
|
+
|
|
228
|
+
Args:
|
|
229
|
+
items: List of input items to add to the history
|
|
230
|
+
"""
|
|
231
|
+
if not items:
|
|
232
|
+
return
|
|
233
|
+
|
|
234
|
+
await self._ensure_tables()
|
|
235
|
+
payload = [
|
|
236
|
+
{
|
|
237
|
+
"session_id": self.session_id,
|
|
238
|
+
"message_data": await self._serialize_item(item),
|
|
239
|
+
}
|
|
240
|
+
for item in items
|
|
241
|
+
]
|
|
242
|
+
|
|
243
|
+
async with self._session_factory() as sess:
|
|
244
|
+
async with sess.begin():
|
|
245
|
+
# Ensure the parent session row exists - use merge for cross-DB compatibility
|
|
246
|
+
# Check if session exists
|
|
247
|
+
existing = await sess.execute(
|
|
248
|
+
select(self._sessions.c.session_id).where(
|
|
249
|
+
self._sessions.c.session_id == self.session_id
|
|
250
|
+
)
|
|
251
|
+
)
|
|
252
|
+
if not existing.scalar_one_or_none():
|
|
253
|
+
# Session doesn't exist, create it
|
|
254
|
+
await sess.execute(
|
|
255
|
+
insert(self._sessions).values({"session_id": self.session_id})
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
# Insert messages in bulk
|
|
259
|
+
await sess.execute(insert(self._messages), payload)
|
|
260
|
+
|
|
261
|
+
# Touch updated_at column
|
|
262
|
+
await sess.execute(
|
|
263
|
+
update(self._sessions)
|
|
264
|
+
.where(self._sessions.c.session_id == self.session_id)
|
|
265
|
+
.values(updated_at=sql_text("CURRENT_TIMESTAMP"))
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
async def pop_item(self) -> TResponseInputItem | None:
|
|
269
|
+
"""Remove and return the most recent item from the session.
|
|
270
|
+
|
|
271
|
+
Returns:
|
|
272
|
+
The most recent item if it exists, None if the session is empty
|
|
273
|
+
"""
|
|
274
|
+
await self._ensure_tables()
|
|
275
|
+
async with self._session_factory() as sess:
|
|
276
|
+
async with sess.begin():
|
|
277
|
+
# Fallback for all dialects - get ID first, then delete
|
|
278
|
+
subq = (
|
|
279
|
+
select(self._messages.c.id)
|
|
280
|
+
.where(self._messages.c.session_id == self.session_id)
|
|
281
|
+
.order_by(self._messages.c.created_at.desc())
|
|
282
|
+
.limit(1)
|
|
283
|
+
)
|
|
284
|
+
res = await sess.execute(subq)
|
|
285
|
+
row_id = res.scalar_one_or_none()
|
|
286
|
+
if row_id is None:
|
|
287
|
+
return None
|
|
288
|
+
# Fetch data before deleting
|
|
289
|
+
res_data = await sess.execute(
|
|
290
|
+
select(self._messages.c.message_data).where(self._messages.c.id == row_id)
|
|
291
|
+
)
|
|
292
|
+
row = res_data.scalar_one_or_none()
|
|
293
|
+
await sess.execute(delete(self._messages).where(self._messages.c.id == row_id))
|
|
294
|
+
|
|
295
|
+
if row is None:
|
|
296
|
+
return None
|
|
297
|
+
try:
|
|
298
|
+
return await self._deserialize_item(row)
|
|
299
|
+
except json.JSONDecodeError:
|
|
300
|
+
return None
|
|
301
|
+
|
|
302
|
+
async def clear_session(self) -> None:
|
|
303
|
+
"""Clear all items for this session."""
|
|
304
|
+
await self._ensure_tables()
|
|
305
|
+
async with self._session_factory() as sess:
|
|
306
|
+
async with sess.begin():
|
|
307
|
+
await sess.execute(
|
|
308
|
+
delete(self._messages).where(self._messages.c.session_id == self.session_id)
|
|
309
|
+
)
|
|
310
|
+
await sess.execute(
|
|
311
|
+
delete(self._sessions).where(self._sessions.c.session_id == self.session_id)
|
|
312
|
+
)
|
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import json
|
|
4
4
|
import time
|
|
5
5
|
from collections.abc import AsyncIterator
|
|
6
|
+
from copy import copy
|
|
6
7
|
from typing import Any, Literal, cast, overload
|
|
7
8
|
|
|
8
9
|
from openai.types.responses.response_usage import InputTokensDetails, OutputTokensDetails
|
|
@@ -20,6 +21,7 @@ except ImportError as _e:
|
|
|
20
21
|
from openai import NOT_GIVEN, AsyncStream, NotGiven
|
|
21
22
|
from openai.types.chat import (
|
|
22
23
|
ChatCompletionChunk,
|
|
24
|
+
ChatCompletionMessageCustomToolCall,
|
|
23
25
|
ChatCompletionMessageFunctionToolCall,
|
|
24
26
|
)
|
|
25
27
|
from openai.types.chat.chat_completion_message import (
|
|
@@ -28,7 +30,6 @@ from openai.types.chat.chat_completion_message import (
|
|
|
28
30
|
ChatCompletionMessage,
|
|
29
31
|
)
|
|
30
32
|
from openai.types.chat.chat_completion_message_function_tool_call import Function
|
|
31
|
-
from openai.types.chat.chat_completion_message_tool_call import ChatCompletionMessageToolCall
|
|
32
33
|
from openai.types.responses import Response
|
|
33
34
|
|
|
34
35
|
from ... import _debug
|
|
@@ -82,7 +83,8 @@ class LitellmModel(Model):
|
|
|
82
83
|
output_schema: AgentOutputSchemaBase | None,
|
|
83
84
|
handoffs: list[Handoff],
|
|
84
85
|
tracing: ModelTracing,
|
|
85
|
-
previous_response_id: str | None,
|
|
86
|
+
previous_response_id: str | None = None, # unused
|
|
87
|
+
conversation_id: str | None = None, # unused
|
|
86
88
|
prompt: Any | None = None,
|
|
87
89
|
) -> ModelResponse:
|
|
88
90
|
with generation_span(
|
|
@@ -171,7 +173,8 @@ class LitellmModel(Model):
|
|
|
171
173
|
output_schema: AgentOutputSchemaBase | None,
|
|
172
174
|
handoffs: list[Handoff],
|
|
173
175
|
tracing: ModelTracing,
|
|
174
|
-
previous_response_id: str | None,
|
|
176
|
+
previous_response_id: str | None = None, # unused
|
|
177
|
+
conversation_id: str | None = None, # unused
|
|
175
178
|
prompt: Any | None = None,
|
|
176
179
|
) -> AsyncIterator[TResponseStreamEvent]:
|
|
177
180
|
with generation_span(
|
|
@@ -300,9 +303,9 @@ class LitellmModel(Model):
|
|
|
300
303
|
|
|
301
304
|
extra_kwargs = {}
|
|
302
305
|
if model_settings.extra_query:
|
|
303
|
-
extra_kwargs["extra_query"] = model_settings.extra_query
|
|
306
|
+
extra_kwargs["extra_query"] = copy(model_settings.extra_query)
|
|
304
307
|
if model_settings.metadata:
|
|
305
|
-
extra_kwargs["metadata"] = model_settings.metadata
|
|
308
|
+
extra_kwargs["metadata"] = copy(model_settings.metadata)
|
|
306
309
|
if model_settings.extra_body and isinstance(model_settings.extra_body, dict):
|
|
307
310
|
extra_kwargs.update(model_settings.extra_body)
|
|
308
311
|
|
|
@@ -366,7 +369,9 @@ class LitellmConverter:
|
|
|
366
369
|
if message.role != "assistant":
|
|
367
370
|
raise ModelBehaviorError(f"Unsupported role: {message.role}")
|
|
368
371
|
|
|
369
|
-
tool_calls: list[
|
|
372
|
+
tool_calls: list[
|
|
373
|
+
ChatCompletionMessageFunctionToolCall | ChatCompletionMessageCustomToolCall
|
|
374
|
+
] | None = (
|
|
370
375
|
[LitellmConverter.convert_tool_call_to_openai(tool) for tool in message.tool_calls]
|
|
371
376
|
if message.tool_calls
|
|
372
377
|
else None
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
+
from ...models.default_models import get_default_model
|
|
1
2
|
from ...models.interface import Model, ModelProvider
|
|
2
3
|
from .litellm_model import LitellmModel
|
|
3
4
|
|
|
5
|
+
# This is kept for backward compatiblity but using get_default_model() method is recommended.
|
|
4
6
|
DEFAULT_MODEL: str = "gpt-4.1"
|
|
5
7
|
|
|
6
8
|
|
|
@@ -18,4 +20,4 @@ class LitellmProvider(ModelProvider):
|
|
|
18
20
|
"""
|
|
19
21
|
|
|
20
22
|
def get_model(self, model_name: str | None) -> Model:
|
|
21
|
-
return LitellmModel(model_name or
|
|
23
|
+
return LitellmModel(model_name or get_default_model())
|
agents/function_schema.py
CHANGED
|
@@ -291,7 +291,7 @@ def function_schema(
|
|
|
291
291
|
# Default factory to empty list
|
|
292
292
|
fields[name] = (
|
|
293
293
|
ann,
|
|
294
|
-
Field(default_factory=list, description=field_description),
|
|
294
|
+
Field(default_factory=list, description=field_description),
|
|
295
295
|
)
|
|
296
296
|
|
|
297
297
|
elif param.kind == param.VAR_KEYWORD:
|
|
@@ -309,7 +309,7 @@ def function_schema(
|
|
|
309
309
|
|
|
310
310
|
fields[name] = (
|
|
311
311
|
ann,
|
|
312
|
-
Field(default_factory=dict, description=field_description),
|
|
312
|
+
Field(default_factory=dict, description=field_description),
|
|
313
313
|
)
|
|
314
314
|
|
|
315
315
|
else:
|
agents/handoffs.py
CHANGED
|
@@ -119,9 +119,9 @@ class Handoff(Generic[TContext, TAgent]):
|
|
|
119
119
|
True, as it increases the likelihood of correct JSON input.
|
|
120
120
|
"""
|
|
121
121
|
|
|
122
|
-
is_enabled: bool | Callable[
|
|
123
|
-
|
|
124
|
-
|
|
122
|
+
is_enabled: bool | Callable[[RunContextWrapper[Any], AgentBase[Any]], MaybeAwaitable[bool]] = (
|
|
123
|
+
True
|
|
124
|
+
)
|
|
125
125
|
"""Whether the handoff is enabled. Either a bool or a Callable that takes the run context and
|
|
126
126
|
agent and returns whether the handoff is enabled. You can use this to dynamically enable/disable
|
|
127
127
|
a handoff based on your context/state."""
|
agents/lifecycle.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
from typing import Any, Generic
|
|
1
|
+
from typing import Any, Generic, Optional
|
|
2
2
|
|
|
3
3
|
from typing_extensions import TypeVar
|
|
4
4
|
|
|
5
5
|
from .agent import Agent, AgentBase
|
|
6
|
+
from .items import ModelResponse, TResponseInputItem
|
|
6
7
|
from .run_context import RunContextWrapper, TContext
|
|
7
8
|
from .tool import Tool
|
|
8
9
|
|
|
@@ -14,6 +15,25 @@ class RunHooksBase(Generic[TContext, TAgent]):
|
|
|
14
15
|
override the methods you need.
|
|
15
16
|
"""
|
|
16
17
|
|
|
18
|
+
async def on_llm_start(
|
|
19
|
+
self,
|
|
20
|
+
context: RunContextWrapper[TContext],
|
|
21
|
+
agent: Agent[TContext],
|
|
22
|
+
system_prompt: Optional[str],
|
|
23
|
+
input_items: list[TResponseInputItem],
|
|
24
|
+
) -> None:
|
|
25
|
+
"""Called just before invoking the LLM for this agent."""
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
async def on_llm_end(
|
|
29
|
+
self,
|
|
30
|
+
context: RunContextWrapper[TContext],
|
|
31
|
+
agent: Agent[TContext],
|
|
32
|
+
response: ModelResponse,
|
|
33
|
+
) -> None:
|
|
34
|
+
"""Called immediately after the LLM call returns for this agent."""
|
|
35
|
+
pass
|
|
36
|
+
|
|
17
37
|
async def on_agent_start(self, context: RunContextWrapper[TContext], agent: TAgent) -> None:
|
|
18
38
|
"""Called before the agent is invoked. Called each time the current agent changes."""
|
|
19
39
|
pass
|
|
@@ -106,6 +126,25 @@ class AgentHooksBase(Generic[TContext, TAgent]):
|
|
|
106
126
|
"""Called after a tool is invoked."""
|
|
107
127
|
pass
|
|
108
128
|
|
|
129
|
+
async def on_llm_start(
|
|
130
|
+
self,
|
|
131
|
+
context: RunContextWrapper[TContext],
|
|
132
|
+
agent: Agent[TContext],
|
|
133
|
+
system_prompt: Optional[str],
|
|
134
|
+
input_items: list[TResponseInputItem],
|
|
135
|
+
) -> None:
|
|
136
|
+
"""Called immediately before the agent issues an LLM call."""
|
|
137
|
+
pass
|
|
138
|
+
|
|
139
|
+
async def on_llm_end(
|
|
140
|
+
self,
|
|
141
|
+
context: RunContextWrapper[TContext],
|
|
142
|
+
agent: Agent[TContext],
|
|
143
|
+
response: ModelResponse,
|
|
144
|
+
) -> None:
|
|
145
|
+
"""Called immediately after the agent receives the LLM response."""
|
|
146
|
+
pass
|
|
147
|
+
|
|
109
148
|
|
|
110
149
|
RunHooks = RunHooksBase[TContext, Agent]
|
|
111
150
|
"""Run hooks when using `Agent`."""
|