@@ -239,6 +239,62 @@ def test_to_dict(self, weather_tool, component_tool, monkeypatch):
239239 },
240240 }
241241
242+ def test_to_dict_with_toolset (self , monkeypatch , weather_tool ):
243+ monkeypatch .setenv ("OPENAI_API_KEY" , "fake-key" )
244+ toolset = Toolset (tools = [weather_tool ])
245+ agent = Agent (chat_generator = OpenAIChatGenerator (), tools = toolset )
246+ serialized_agent = agent .to_dict ()
247+ assert serialized_agent == {
248+ "type" : "haystack.components.agents.agent.Agent" ,
249+ "init_parameters" : {
250+ "chat_generator" : {
251+ "type" : "haystack.components.generators.chat.openai.OpenAIChatGenerator" ,
252+ "init_parameters" : {
253+ "model" : "gpt-4o-mini" ,
254+ "streaming_callback" : None ,
255+ "api_base_url" : None ,
256+ "organization" : None ,
257+ "generation_kwargs" : {},
258+ "api_key" : {"type" : "env_var" , "env_vars" : ["OPENAI_API_KEY" ], "strict" : True },
259+ "timeout" : None ,
260+ "max_retries" : None ,
261+ "tools" : None ,
262+ "tools_strict" : False ,
263+ "http_client_kwargs" : None ,
264+ },
265+ },
266+ "tools" : {
267+ "type" : "haystack.tools.toolset.Toolset" ,
268+ "data" : {
269+ "tools" : [
270+ {
271+ "type" : "haystack.tools.tool.Tool" ,
272+ "data" : {
273+ "name" : "weather_tool" ,
274+ "description" : "Provides weather information for a given location." ,
275+ "parameters" : {
276+ "type" : "object" ,
277+ "properties" : {"location" : {"type" : "string" }},
278+ "required" : ["location" ],
279+ },
280+ "function" : "test_agent.weather_function" ,
281+ "outputs_to_string" : None ,
282+ "inputs_from_state" : None ,
283+ "outputs_to_state" : None ,
284+ },
285+ }
286+ ]
287+ },
288+ },
289+ "system_prompt" : None ,
290+ "exit_conditions" : ["text" ],
291+ "state_schema" : {},
292+ "max_agent_steps" : 100 ,
293+ "raise_on_tool_invocation_failure" : False ,
294+ "streaming_callback" : None ,
295+ },
296+ }
297+
242298 def test_from_dict (self , weather_tool , component_tool , monkeypatch ):
243299 monkeypatch .setenv ("OPENAI_API_KEY" , "fake-key" )
244300 data = {
@@ -318,6 +374,67 @@ def test_from_dict(self, weather_tool, component_tool, monkeypatch):
318374 "messages" : {"handler" : merge_lists , "type" : List [ChatMessage ]},
319375 }
320376
377+ def test_from_dict_with_toolset (self , monkeypatch ):
378+ monkeypatch .setenv ("OPENAI_API_KEY" , "fake-key" )
379+ data = {
380+ "type" : "haystack.components.agents.agent.Agent" ,
381+ "init_parameters" : {
382+ "chat_generator" : {
383+ "type" : "haystack.components.generators.chat.openai.OpenAIChatGenerator" ,
384+ "init_parameters" : {
385+ "model" : "gpt-4o-mini" ,
386+ "streaming_callback" : None ,
387+ "api_base_url" : None ,
388+ "organization" : None ,
389+ "generation_kwargs" : {},
390+ "api_key" : {"type" : "env_var" , "env_vars" : ["OPENAI_API_KEY" ], "strict" : True },
391+ "timeout" : None ,
392+ "max_retries" : None ,
393+ "tools" : None ,
394+ "tools_strict" : False ,
395+ "http_client_kwargs" : None ,
396+ },
397+ },
398+ "tools" : {
399+ "type" : "haystack.tools.toolset.Toolset" ,
400+ "data" : {
401+ "tools" : [
402+ {
403+ "type" : "haystack.tools.tool.Tool" ,
404+ "data" : {
405+ "name" : "weather_tool" ,
406+ "description" : "Provides weather information for a given location." ,
407+ "parameters" : {
408+ "type" : "object" ,
409+ "properties" : {"location" : {"type" : "string" }},
410+ "required" : ["location" ],
411+ },
412+ "function" : "test_agent.weather_function" ,
413+ "outputs_to_string" : None ,
414+ "inputs_from_state" : None ,
415+ "outputs_to_state" : None ,
416+ },
417+ }
418+ ]
419+ },
420+ },
421+ "system_prompt" : None ,
422+ "exit_conditions" : ["text" ],
423+ "state_schema" : {},
424+ "max_agent_steps" : 100 ,
425+ "raise_on_tool_invocation_failure" : False ,
426+ "streaming_callback" : None ,
427+ },
428+ }
429+ agent = Agent .from_dict (data )
430+ assert isinstance (agent , Agent )
431+ assert isinstance (agent .chat_generator , OpenAIChatGenerator )
432+ assert agent .chat_generator .model == "gpt-4o-mini"
433+ assert agent .chat_generator .api_key == Secret .from_env_var ("OPENAI_API_KEY" )
434+ assert isinstance (agent .tools , Toolset )
435+ assert agent .tools [0 ].function is weather_function
436+ assert agent .exit_conditions == ["text" ]
437+
321438 def test_serde (self , weather_tool , component_tool , monkeypatch ):
322439 monkeypatch .setenv ("FAKE_OPENAI_KEY" , "fake-key" )
323440 generator = OpenAIChatGenerator (api_key = Secret .from_env_var ("FAKE_OPENAI_KEY" ))
0 commit comments