Skip to content

Commit 30f5aef

Browse files
authored
Update agent.py
1 parent bb77759 commit 30f5aef

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

src/agents/agent.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ class Agent(AgentBase, Generic[TContext]):
222222
to True. This ensures that the agent doesn't enter an infinite loop of tool usage."""
223223

224224
def __post_init__(self):
225+
from typing import get_origin
226+
225227
if not isinstance(self.name, str):
226228
raise TypeError(f"Agent name must be a string, got {type(self.name).__name__}")
227229

@@ -256,21 +258,26 @@ def __post_init__(self):
256258

257259
if (
258260
self.prompt is not None
259-
and not isinstance(self.prompt, dict)
260261
and not callable(self.prompt)
262+
and not hasattr(self.prompt, "get")
261263
):
262264
raise TypeError(
263-
f"Agent prompt must be a Prompt, DynamicPromptFunction, callable, or None, "
265+
f"Agent prompt must be a Prompt, DynamicPromptFunction, or None, "
264266
f"got {type(self.prompt).__name__}"
265267
)
266268

267269
if not isinstance(self.handoffs, list):
268270
raise TypeError(f"Agent handoffs must be a list, got {type(self.handoffs).__name__}")
269271

270-
if self.model is not None and not isinstance(self.model, (str, Model)):
271-
raise TypeError(
272-
f"Agent model must be a string, Model, or None, got {type(self.model).__name__}"
273-
)
272+
if self.model is not None and not isinstance(self.model, str):
273+
from .models.interface import Model
274+
275+
if not isinstance(self.model, Model):
276+
raise TypeError(
277+
f"Agent model must be a string, Model, or None, got {type(self.model).__name__}"
278+
)
279+
280+
from .model_settings import ModelSettings
274281

275282
if not isinstance(self.model_settings, ModelSettings):
276283
raise TypeError(
@@ -289,35 +296,36 @@ def __post_init__(self):
289296
f"got {type(self.output_guardrails).__name__}"
290297
)
291298

292-
if self.output_type is not None and not isinstance(
293-
self.output_type, (type, AgentOutputSchemaBase)
294-
):
295-
raise TypeError(
296-
f"Agent output_type must be a type, AgentOutputSchemaBase, or None, "
297-
f"got {type(self.output_type).__name__}"
298-
)
299+
if self.output_type is not None:
300+
from .agent_output import AgentOutputSchemaBase
301+
302+
if not (
303+
isinstance(self.output_type, (type, AgentOutputSchemaBase))
304+
or get_origin(self.output_type) is not None
305+
):
306+
raise TypeError(
307+
f"Agent output_type must be a type, AgentOutputSchemaBase, or None, "
308+
f"got {type(self.output_type).__name__}"
309+
)
299310

300-
# Fixed hooks validation - use AgentHooksBase instead of generic AgentHooks
301311
if self.hooks is not None:
302-
from .lifecycle import AgentHooksBase
312+
from .lifecycle import AgentHooks
303313

304-
if not isinstance(self.hooks, AgentHooksBase):
314+
if not isinstance(self.hooks, AgentHooks):
305315
raise TypeError(
306316
f"Agent hooks must be an AgentHooks instance or None, "
307317
f"got {type(self.hooks).__name__}"
308318
)
309319

310-
if (
311-
not (
312-
isinstance(self.tool_use_behavior, str)
313-
and self.tool_use_behavior in ["run_llm_again", "stop_on_first_tool"]
314-
)
315-
and not isinstance(self.tool_use_behavior, dict)
316-
and not callable(self.tool_use_behavior)
320+
if not (
321+
self.tool_use_behavior == "run_llm_again"
322+
or self.tool_use_behavior == "stop_on_first_tool"
323+
or isinstance(self.tool_use_behavior, list)
324+
or callable(self.tool_use_behavior)
317325
):
318326
raise TypeError(
319327
f"Agent tool_use_behavior must be 'run_llm_again', 'stop_on_first_tool', "
320-
f"StopAtTools dict, or callable, got {type(self.tool_use_behavior).__name__}"
328+
f"a list of tool names, or a callable, got {type(self.tool_use_behavior).__name__}"
321329
)
322330

323331
if not isinstance(self.reset_tool_choice, bool):

0 commit comments

Comments
 (0)