#Json Format Assistant API

1 messages · Page 1 of 1 (latest)

fierce linden
#

Im using assistant, i want it to return the response in json format. Im using file search feature

woeful tigerBOT
#

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:


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

fierce linden
#

@woeful tiger im using nodejs where to add json format when add messages to thread ? or when runnign the thread or when creating assistant ?

woeful tigerBOT
#

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:


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

fierce linden
#

@woeful tiger does assistant api support json format ?

woeful tigerBOT
#

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:


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

white gyro
#

@woeful tiger will this work for code_interpret and file search

woeful tigerBOT
#

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:


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

white gyro
#

@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
}

woeful tigerBOT
#

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:


Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: