azure-sdk-for-net icon indicating copy to clipboard operation
azure-sdk-for-net copied to clipboard

Setting Response Format to JSON for OpenAI Assistant

Open raysuelzer opened this issue 1 year ago • 5 comments

Library name and version

Azure.AI.OpenAI.Assistants_1.0.0-beta.3

Query/Question

I cannot seem to find anywhere in the Assistants SDK to set the "response format" when using the OpenAI SDKs. This is useful to ensure that the API returns valid JSON. Sorry if this exists but I am missing it.

From OpenAI:

https://platform.openai.com/docs/api-reference/chat/create#chat-create-response_format

response_format
object

Optional
An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than gpt-3.5-turbo-1106.

Setting to { "type": "json_object" } enables JSON mode, which guarantees the message the model generates is valid JSON.

Important: 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. Also note that the message content may be partially cut off if finish_reason="length", which indicates the generation exceeded max_tokens or the conversation exceeded the max context length.

Environment

No response

raysuelzer avatar Apr 10 '24 00:04 raysuelzer

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @jpalvarezl @trrwilson.

github-actions[bot] avatar Apr 10 '24 00:04 github-actions[bot]

Any help on this would be appreciated. I'm having some real issues gettng the assistant to return JSON responses.

raysuelzer avatar May 19 '24 00:05 raysuelzer

@raysuelzer as a temporary workaround, you can visit https://platform.openai.com/assistants using your account (the one tied to the API key you are using to create assistants), click on your assistant and toggle 'JSON object' under Response format

nighby avatar Jun 01 '24 05:06 nighby

Still missing in 1.0.0-beta.4

medoni avatar Jun 15 '24 06:06 medoni

Ideally, this would also support the json_schema value introduced with structured outputs, not just json_object: https://openai.com/index/introducing-structured-outputs-in-the-api/.

FWIW as of 2.0.0-beta2, neither value is supported.

TomasHubelbauer avatar Aug 21 '24 10:08 TomasHubelbauer