Firstly, thank you for this. It really does need to be added to the documentation. The docs are not clear at all.
It should be noted that all tools must be of type function when response_format is of type json_schema.
This is/was in the documentation in some form but it is not made clear. The documentation at https://platform.openai.com/docs/guides/structured-outputs/introduction makes it sound like you can use it When using function callingorWhen using a json_schema response format but that does not apply to assistants.
I tried implementing json_schema with tools=[{"type": "file_search"}] and it is still not a thing. I’ve needed this feature so it’s more like chat completions for a while but right now; I’m having to convince the assistant API to respond with JSON through some kludgery instead, and then dealing with broken JSON output through exception handling.
I’m dealing with the same problem - not being able to use the tools. One workaround is to do two consecutive runs for a given prompt - first one to solve the problem using whatever the tools you need to use. The second one with the structured output and the prompt “please output the results as json” - the results from the first run will be in the immediate message history so should work ok.
I am still having issues with setting the assistant with the structured outputs.
First, it’s unclear where we should set it(nothing was mentioned in the documentation). in the creation of the assistant/messages/threads?
secondly, I tried out the suggestion above as follows:
openai.BadRequestError: Error code: 400 - {'error': {'message': 'Invalid tools: all tools must be of type `function` when `response_format` is of type `json_schema`.', 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}
The answer is in the error message. You can use any other tools than your own functions. So no code interpreter, no file search. you can set tools = in the call to create the run - this will override whatever tools you have set up on the agent. This way you can at least check, that the structured output part is working. To use tools split your workflow into two parts - create a run where you try to solve the task given by the prompt with all the tools you need. In the second run disable all the tools but set the structured output schema and in the prompts ask the agent to “please output the results as the given json” or something of the like.