#Json Format Assistant API
1 messages · Page 1 of 1 (latest)
Hi @fierce linden. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
To get responses in JSON format from the Assistant API, you need to set the response_format to json_object in your API call. This will ensure that the message the model generates is valid JSON.
Here is an example of how you can set the response_format:
response_format = {
"type": "json_object"
}
You can include this in your API call when creating, modifying, or retrieving an assistant.
However, it's important to note that when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request.
For example, your system message could be:
system_message = {
"role": "system",
"content": "You are an excellent assistant. Use the uploaded files to best respond to a senior project manager’s questions. Please respond with a JSON object following this schema: {...}"
}
And your user message could be:
user_message = {
"role": "user",
"content": "Follow the system instructions."
}
Remember to replace {...} with your desired JSON schema.
Sources:
- OpenAI API - Assistants > Methods > getAssistant > Responses Part 4 (https://platform.openai.com/docs/api-reference)
- OpenAI API - Assistants > Methods > createAssistant > Responses Part 4 (https://platform.openai.com/docs/api-reference)
- OpenAI API - Assistants > Methods > modifyAssistant > Responses Part 4 (https://platform.openai.com/docs/api-reference)
- Forum Post: Assistant API function: responses in markdown and not JSON > Official Answer (https://community.openai.com/t/711202)
- Forum Post: Extracting Structured Data from Transcribed Voicemails > Official Answer (https://community.openai.com/t/682652)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@fierce linden kapa.ai is still learning and improving, please let us know how it did by reacting below
@woeful tiger im using nodejs where to add json format when add messages to thread ? or when runnign the thread or when creating assistant ?
Let me check the knowledge base for you. One moment! :books:
In Node.js, when you're using the OpenAI Assistant API, you can specify the response_format when you're running the thread. Here's an example of how you might do this:
const runData = {
'thread_id': threadId,
'response_format': { 'type': 'json_object' }
};
const runResponse = callAPI('threads/' + threadId + '/run', runData);
In this example, callAPI is a function that makes the actual API call. You would replace this with your actual function for making API calls.
Remember, when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request.
Sources:
- Forum Post: Targeting an API assistant in PHP > Official Answer Part 2 (https://community.openai.com/t/739180)
- OpenAI API - Assistants > Methods > createThreadAndRun > Request body Part 10 (https://platform.openai.com/docs/api-reference)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@fierce linden kapa.ai is still learning and improving, please let us know how it did by reacting below
@woeful tiger does assistant api support json format ?
Let me check the knowledge base for you. One moment! :books:
Yes, the Assistant API does support JSON format. You can specify the response_format as json_object when making API calls to the Assistant API. This will ensure that the message the model generates is valid JSON.
However, it's important to note that when using JSON mode, you must also instruct the model to produce JSON yourself via a system or user message. Without this, the model may generate an unending stream of whitespace until the generation reaches the token limit, resulting in a long-running and seemingly "stuck" request.
Here is an example of how you can set the response_format:
response_format = {
"type": "json_object"
}
You can include this in your API call when creating, modifying, or retrieving an assistant.
Sources:
- Help Article > En > Articles > Is there a limit to how many assistants I can create? (https://help.openai.com/en/articles/8550641-assistants-api-v2-faq#h_1af2aff3cc)
- OpenAI API - Assistants > Methods > createRun > Request body Part 8 (https://platform.openai.com/docs/api-reference)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@fierce linden kapa.ai is still learning and improving, please let us know how it did by reacting below
@woeful tiger will this work for code_interpret and file search
Let me check the knowledge base for you. One moment! :books:
Yes, the response_format as json_object should work for all tools used with the Assistant API, including code_interpreter and file_search. When you make an API call, the response will be in JSON format.
However, please note that the maximum number of files you can attach to code_interpreter is 20, and for file_search, it's 10,000. Each file can be at most 512 MB in size and have a maximum of 5,000,000 tokens. By default, the size of all the files uploaded in your project cannot exceed 100 GB, but you can reach out to OpenAI's support team to increase this limit.
Sources:
- Docs > Assistants > Deep-dive > Assistants API Deep dive Beta > Creating Assistants Part 2 (https://platform.openai.com/docs/assistants/deep-dive/agents)
- OpenAI API - Assistants > Methods > createRun > Request body Part 8 (https://platform.openai.com/docs/api-reference)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@white gyro kapa.ai is still learning and improving, please let us know how it did by reacting below
@woeful tiger {
"status": false,
"message": "An error occurred while creating the assistant.Error code: 400 - {'error': {'message': 'Invalid tools: all tools must be of type function when response_format is of type json_object.', 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}",
"data": null
}
Let me check the knowledge base for you. One moment! :books:
The error message you're seeing indicates that when the response_format is set to json_object, all tools must be of type function. This means that if you're using the response_format as json_object, you cannot use tools like code_interpreter or file_search.
Here's the relevant part of the error message:
{
"error": {
"message": "Invalid tools: all tools must be of type `function` when `response_format` is of type `json_object`.",
"type": "invalid_request_error",
"param": "response_format",
"code": None
}
}
So, if you want to use json_object as the response_format, you'll need to ensure that all the tools you're using are of type function.
Sources:
- Forum Post: Response_format set to JSON not accepted by openai.beta.threads.createAndRun > Original post (https://community.openai.com/t/725937)
- OpenAI API - Assistants > Methods > createAssistant > Request body Part 7 (https://platform.openai.com/docs/api-reference)