@@ -222,6 +222,8 @@ class Agent(AgentBase, Generic[TContext]):
222
222
to True. This ensures that the agent doesn't enter an infinite loop of tool usage."""
223
223
224
224
def __post_init__ (self ):
225
+ from typing import get_origin
226
+
225
227
if not isinstance (self .name , str ):
226
228
raise TypeError (f"Agent name must be a string, got { type (self .name ).__name__ } " )
227
229
@@ -256,21 +258,26 @@ def __post_init__(self):
256
258
257
259
if (
258
260
self .prompt is not None
259
- and not isinstance (self .prompt , dict )
260
261
and not callable (self .prompt )
262
+ and not hasattr (self .prompt , "get" )
261
263
):
262
264
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, "
264
266
f"got { type (self .prompt ).__name__ } "
265
267
)
266
268
267
269
if not isinstance (self .handoffs , list ):
268
270
raise TypeError (f"Agent handoffs must be a list, got { type (self .handoffs ).__name__ } " )
269
271
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
274
281
275
282
if not isinstance (self .model_settings , ModelSettings ):
276
283
raise TypeError (
@@ -289,35 +296,36 @@ def __post_init__(self):
289
296
f"got { type (self .output_guardrails ).__name__ } "
290
297
)
291
298
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
+ )
299
310
300
- # Fixed hooks validation - use AgentHooksBase instead of generic AgentHooks
301
311
if self .hooks is not None :
302
- from .lifecycle import AgentHooksBase
312
+ from .lifecycle import AgentHooks
303
313
304
- if not isinstance (self .hooks , AgentHooksBase ):
314
+ if not isinstance (self .hooks , AgentHooks ):
305
315
raise TypeError (
306
316
f"Agent hooks must be an AgentHooks instance or None, "
307
317
f"got { type (self .hooks ).__name__ } "
308
318
)
309
319
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 )
317
325
):
318
326
raise TypeError (
319
327
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__ } "
321
329
)
322
330
323
331
if not isinstance (self .reset_tool_choice , bool ):
0 commit comments