@@ -222,9 +222,118 @@ 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
230
+ if self .handoff_description is not None and not isinstance (self .handoff_description , str ):
231
+ raise TypeError (
232
+ f"Agent handoff_description must be a string or None, "
233
+ f"got { type (self .handoff_description ).__name__ } "
234
+ )
235
+
236
+ if not isinstance (self .tools , list ):
237
+ raise TypeError (f"Agent tools must be a list, got { type (self .tools ).__name__ } " )
238
+
239
+ if not isinstance (self .mcp_servers , list ):
240
+ raise TypeError (
241
+ f"Agent mcp_servers must be a list, got { type (self .mcp_servers ).__name__ } "
242
+ )
243
+
244
+ if not isinstance (self .mcp_config , dict ):
245
+ raise TypeError (
246
+ f"Agent mcp_config must be a dict, got { type (self .mcp_config ).__name__ } "
247
+ )
248
+
249
+ if (
250
+ self .instructions is not None
251
+ and not isinstance (self .instructions , str )
252
+ and not callable (self .instructions )
253
+ ):
254
+ raise TypeError (
255
+ f"Agent instructions must be a string, callable, or None, "
256
+ f"got { type (self .instructions ).__name__ } "
257
+ )
258
+
259
+ if (
260
+ self .prompt is not None
261
+ and not callable (self .prompt )
262
+ and not hasattr (self .prompt , "get" )
263
+ ):
264
+ raise TypeError (
265
+ f"Agent prompt must be a Prompt, DynamicPromptFunction, or None, "
266
+ f"got { type (self .prompt ).__name__ } "
267
+ )
268
+
269
+ if not isinstance (self .handoffs , list ):
270
+ raise TypeError (f"Agent handoffs must be a list, got { type (self .handoffs ).__name__ } " )
271
+
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
+ if not isinstance (self .model_settings , ModelSettings ):
281
+ raise TypeError (
282
+ f"Agent model_settings must be a ModelSettings instance, "
283
+ f"got { type (self .model_settings ).__name__ } "
284
+ )
285
+
286
+ if not isinstance (self .input_guardrails , list ):
287
+ raise TypeError (
288
+ f"Agent input_guardrails must be a list, got { type (self .input_guardrails ).__name__ } "
289
+ )
290
+
291
+ if not isinstance (self .output_guardrails , list ):
292
+ raise TypeError (
293
+ f"Agent output_guardrails must be a list, "
294
+ f"got { type (self .output_guardrails ).__name__ } "
295
+ )
296
+
297
+ if self .output_type is not None :
298
+ from .agent_output import AgentOutputSchemaBase
299
+
300
+ if not (
301
+ isinstance (self .output_type , (type , AgentOutputSchemaBase ))
302
+ or get_origin (self .output_type ) is not None
303
+ ):
304
+ raise TypeError (
305
+ f"Agent output_type must be a type, AgentOutputSchemaBase, or None, "
306
+ f"got { type (self .output_type ).__name__ } "
307
+ )
308
+
309
+ if self .hooks is not None :
310
+ from .lifecycle import AgentHooksBase
311
+
312
+ if not isinstance (self .hooks , AgentHooksBase ):
313
+ raise TypeError (
314
+ f"Agent hooks must be an AgentHooks instance or None, "
315
+ f"got { type (self .hooks ).__name__ } "
316
+ )
317
+
318
+ if (
319
+ not (
320
+ isinstance (self .tool_use_behavior , str )
321
+ and self .tool_use_behavior in ["run_llm_again" , "stop_on_first_tool" ]
322
+ )
323
+ and not isinstance (self .tool_use_behavior , dict )
324
+ and not callable (self .tool_use_behavior )
325
+ ):
326
+ raise TypeError (
327
+ f"Agent tool_use_behavior must be 'run_llm_again', 'stop_on_first_tool', "
328
+ f"StopAtTools dict, or callable, got { type (self .tool_use_behavior ).__name__ } "
329
+ )
330
+
331
+ if not isinstance (self .reset_tool_choice , bool ):
332
+ raise TypeError (
333
+ f"Agent reset_tool_choice must be a boolean, "
334
+ f"got { type (self .reset_tool_choice ).__name__ } "
335
+ )
336
+
228
337
def clone (self , ** kwargs : Any ) -> Agent [TContext ]:
229
338
"""Make a copy of the agent, with the given arguments changed. For example, you could do:
230
339
```
0 commit comments