Any tips on prompting Assistant API to return the entire response in JSON?

I’ve been getting mixed results with how the Assistant API is returning responses when asked to output it’s entire response in JSON format. Occasionally, it will do just that but then, almost randomly, output a detailed response in standard text and then just a summary in JSON at the end. Pretty sure I’m either not prompting it correctly, or I need to be more specific in the Assistant instructions. Here’s an example prompt:

Analyze the text and provide the following: 1) A summary of the text. 2) Word count 3) Areas for improvement. Output your entire response in JSON format with fields: “An ID I pass to it”, “Summary”, “Word Count”.

Any guidance on correctly asking it to output in JSON format consistently is greatly appreciated!

AI can’t count words well. A simple function easily can. If it were to work, you’d have to be clearer about what words are being counted.

The correct JSON term is “key” for the data name, and “value” for data. That allows clear communication with the AI.

Extremely explicit definition? How about this schema I constructed for another forum answer, not only exacting and demonstrative, but a form that can itself validate the AI output.

The example schema shows the AI any user-directed response must also be only in the container (and don’t expect the last numbers to be faithfully answered).

You are a helpful AI assistant.
Your output is to an API.
Response to user and metadata will be extracted from output json.
Except for tool calls, create only valid json complying to schema.

// json output schema
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "message": {
      "type": "object",
      "properties": {
        "assistant": {
          "type": "string",
          "description": "the response to the user",
          "example": "I am glad to help!"
        },
        "topic": {
          "type": "string",
          "description": "subject of recent discussion",
          "example": "baseball"
        },
        "user_mood": {
          "type": "string",
          "description": "user is happy, upset, etc",
          "example": "neutral"
        }
      },
      "required": [
        "assistant",
        "topic",
        "user_mood"
      ]
    },
    "length_of_conversation_turns": {
      "type": "number",
       "description": "total user and assistant conversation exchange turns"
    },
    "conversation_turns_on_topic": {
      "type": "number",
      "description": "number of recent turns engaging newest topic"
    }
  },
  "required": [
    "message",
    "length_of_conversation_turns",
    "conversation_turns_on_topic"
  ]
}
2 Likes

That worked great, thank you!

And word counting was just an example, I’m not actually doing that :slight_smile: