@@ -223,6 +223,119 @@ class Agent(AgentBase, Generic[TContext]):
223
223
"""Whether to reset the tool choice to the default value after a tool has been called. Defaults
224
224
to True. This ensures that the agent doesn't enter an infinite loop of tool usage."""
225
225
226
+ def __post_init__ (self ):
227
+ from typing import get_origin
228
+
229
+ if not isinstance (self .name , str ):
230
+ raise TypeError (f"Agent name must be a string, got { type (self .name ).__name__ } " )
231
+
232
+ if self .handoff_description is not None and not isinstance (self .handoff_description , str ):
233
+ raise TypeError (
234
+ f"Agent handoff_description must be a string or None, "
235
+ f"got { type (self .handoff_description ).__name__ } "
236
+ )
237
+
238
+ if not isinstance (self .tools , list ):
239
+ raise TypeError (f"Agent tools must be a list, got { type (self .tools ).__name__ } " )
240
+
241
+ if not isinstance (self .mcp_servers , list ):
242
+ raise TypeError (
243
+ f"Agent mcp_servers must be a list, got { type (self .mcp_servers ).__name__ } "
244
+ )
245
+
246
+ if not isinstance (self .mcp_config , dict ):
247
+ raise TypeError (
248
+ f"Agent mcp_config must be a dict, got { type (self .mcp_config ).__name__ } "
249
+ )
250
+
251
+ if (
252
+ self .instructions is not None
253
+ and not isinstance (self .instructions , str )
254
+ and not callable (self .instructions )
255
+ ):
256
+ raise TypeError (
257
+ f"Agent instructions must be a string, callable, or None, "
258
+ f"got { type (self .instructions ).__name__ } "
259
+ )
260
+
261
+ if (
262
+ self .prompt is not None
263
+ and not callable (self .prompt )
264
+ and not hasattr (self .prompt , "get" )
265
+ ):
266
+ raise TypeError (
267
+ f"Agent prompt must be a Prompt, DynamicPromptFunction, or None, "
268
+ f"got { type (self .prompt ).__name__ } "
269
+ )
270
+
271
+ if not isinstance (self .handoffs , list ):
272
+ raise TypeError (f"Agent handoffs must be a list, got { type (self .handoffs ).__name__ } " )
273
+
274
+ if self .model is not None and not isinstance (self .model , str ):
275
+ from .models .interface import Model
276
+
277
+ if not isinstance (self .model , Model ):
278
+ raise TypeError (
279
+ f"Agent model must be a string, Model, or None, got { type (self .model ).__name__ } "
280
+ )
281
+
282
+ if not isinstance (self .model_settings , ModelSettings ):
283
+ raise TypeError (
284
+ f"Agent model_settings must be a ModelSettings instance, "
285
+ f"got { type (self .model_settings ).__name__ } "
286
+ )
287
+
288
+ if not isinstance (self .input_guardrails , list ):
289
+ raise TypeError (
290
+ f"Agent input_guardrails must be a list, got { type (self .input_guardrails ).__name__ } "
291
+ )
292
+
293
+ if not isinstance (self .output_guardrails , list ):
294
+ raise TypeError (
295
+ f"Agent output_guardrails must be a list, "
296
+ f"got { type (self .output_guardrails ).__name__ } "
297
+ )
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
+ )
310
+
311
+ if self .hooks is not None :
312
+ from .lifecycle import AgentHooksBase
313
+
314
+ if not isinstance (self .hooks , AgentHooksBase ):
315
+ raise TypeError (
316
+ f"Agent hooks must be an AgentHooks instance or None, "
317
+ f"got { type (self .hooks ).__name__ } "
318
+ )
319
+
320
+ if (
321
+ not (
322
+ isinstance (self .tool_use_behavior , str )
323
+ and self .tool_use_behavior in ["run_llm_again" , "stop_on_first_tool" ]
324
+ )
325
+ and not isinstance (self .tool_use_behavior , dict )
326
+ and not callable (self .tool_use_behavior )
327
+ ):
328
+ raise TypeError (
329
+ f"Agent tool_use_behavior must be 'run_llm_again', 'stop_on_first_tool', "
330
+ f"StopAtTools dict, or callable, got { type (self .tool_use_behavior ).__name__ } "
331
+ )
332
+
333
+ if not isinstance (self .reset_tool_choice , bool ):
334
+ raise TypeError (
335
+ f"Agent reset_tool_choice must be a boolean, "
336
+ f"got { type (self .reset_tool_choice ).__name__ } "
337
+ )
338
+
226
339
def clone (self , ** kwargs : Any ) -> Agent [TContext ]:
227
340
"""Make a copy of the agent, with the given arguments changed.
228
341
Notes:
0 commit comments