JSON Formatting help: Anthropic's Claude AI Model

I need some help from all you JSON wizards out there…

I’ve recently added the Claude AI model from Anthropic into my app. It’s so much faster to work with than GPT, but it has some quirks that make it a little more difficult to work with in terms of the JSON call.

Claude’s API requires the formatting of a request to be:

{
    "prompt": "\n\nHuman: [this is where the human's prompt goes]\n\nAssistant:"
    "model": <model>,
    "stop_sequences": [
        "\n\nHuman:"
    ]
}

Now, with OpenAI, I would just wrap the user’s prompt in a :formatted-as-JSON-safe and that would get rid of any JSON-breaking characters (most times… this is another topic altogether). But since Claude requires the \n\nHuman: and \n\nAssistant: parts inside the request, doing a “formatted-as-JSON-safe” breaks that because it escapes the necessary \n\n formatting.

I’m pulling my hair out trying to figure out how to format these requests so my app users stop getting formatting error messages. Doing a simple :find&replace for things like quotation marks just isn’t working.

Any ideas?

Do you have any documentation from Anthropic? For the find&replace you’d only have to find " and replace with '.

Here’s a link to their documentation: API Reference - Docs

You would think that finding " and replacing would do it, but it doesn’t. There are many other special characters (it seems) that break the JSON call.

Have you tried this while using find & replace on the ‘human_message’ variable?

cc’ing @project

Solution 1
Could you use regex? First, format as JSON safe. Then, extract with Regex. The regex code .(.*). will match everything except the first and last characters which are the opening and closing quotation marks (highlighted in green). Do this for the Human and Assistant prompts.

Solution 2
Alternatively, have a state on your page that stores promptText. When you want to send a message, set state promptText to Arbitrary text:

\n\nHuman: humanPromptInput's value\n\nAssistant: assistantTextHere

Then send Arbitrary text:formatted as JSON-safe to the promptText parameter in the API call:

{
"prompt": <promptText>
"model": <model>,
"stop_sequences": [
    "\n\nHuman:"
]
}

how long did it take for you to get accepted for API use?

More than a month I think.

This topic was automatically closed after 70 days. New replies are no longer allowed.