phidatahq / phidata

Build AI Agents with memory, knowledge, tools and reasoning. Chat with them using a beautiful Agent UI.
https://docs.phidata.com
Mozilla Public License 2.0
15.58k stars 2.14k forks source link

search_knowledge_base / other inbuilt tools not woking with structured_outputs=True #1351

Open debianmaster opened 4 weeks ago

debianmaster commented 4 weeks ago

Following error occuers when structured_outputs=True and search_knowledge_base tool is used

INFO     Table 'ai.recipes' dropped successfully.                                                                                                                         
INFO     Creating collection                                                                                                                                              
INFO     Loading knowledge base                                                                                                                                           
ERROR    Error from OpenAI API: `search_knowledge_base` is not strict. Only `strict` function tools can be auto-parsed                                                    
▰▱▱▱▱▱▱ Thinking...
Traceback (most recent call last):
  File "/Users/jonagamchakradharrao/phidata-ex/sre-agents.py", line 135, in <module>
    architect_agent.print_response(error_log, stream=False)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/phi/agent/agent.py", line 2698, in print_response
    run_response = self.run(message=message, messages=messages, stream=False, **kwargs)
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/phi/agent/agent.py", line 1817, in run
    run_response: RunResponse = next(
                                ~~~~^
        self._run(
        ^^^^^^^^^^
    ...<6 lines>...
        )
        ^
    )
    ^
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/phi/agent/agent.py", line 1660, in _run
    model_response = self.model.response(messages=messages_for_model)
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/phi/model/openai/chat.py", line 558, in response
    response: Union[ChatCompletion, ParsedChatCompletion] = self.invoke(messages=messages)
                                                            ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/phi/model/openai/chat.py", line 323, in invoke
    return self.get_client().chat.completions.create(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        model=self.id,
        ^^^^^^^^^^^^^^
        messages=[m.to_dict() for m in messages],  # type: ignore
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        **self.request_kwargs,
        ^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/openai/_utils/_utils.py", line 274, in wrapper
    return func(*args, **kwargs)
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/openai/resources/chat/completions.py", line 814, in create
    validate_response_format(response_format)
    ~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
  File "/Users/jonagamchakradharrao/.venvs/aienv/lib/python3.13/site-packages/openai/resources/chat/completions.py", line 1715, in validate_response_format
    raise TypeError(
        "You tried to pass a `BaseModel` class to `chat.completions.create()`; You must use `beta.chat.completions.parse()` instead"
    )
TypeError: You tried to pass a `BaseModel` class to `chat.completions.create()`; You must use `beta.chat.completions.parse()` instead
manthanguptaa commented 4 weeks ago

Hey @debianmaster, can you please share your code so that I can help you better?

lukasvanek1 commented 3 weeks ago

Try switching from chat.completions.create() to beta.chat.completions.parse()?

manthanguptaa commented 3 weeks ago

Hey @debianmaster, structured output right now doesn't work with tools and the team is working on adding this feature. Thank you for pointing this out!