Sending Claude message history with each API call

Hi there, I’m trying to set up Claude Sonnet through the API connector. I want to include message history with each API call. The messages are being stored in our bubble database already. How do I modify this JSON body to do that?

{
“model”: “claude-3-5-sonnet-20240620”,
“max_tokens”: 1024,
“system”: “only reply in Spanish”,
“messages”: [
{“role”: “user”, “content”: “Hello there.”},
{“role”: “assistant”, “content”: “Hi, I’m Claude. How can I help you?”},
{“role”: “user”, “content”: “Can you explain LLMs in plain English?”}
]
}

Hi! You can use the :format as text operator in Bubble. This is how it can be done:

{
“model”: “claude-3-5-sonnet-20240620”,
“max_tokens”: 1024,
“system”: “only reply in Spanish”,
“messages”: [
Do a search for messages:format as text
]
}

Within :format as text, you can do the following:

Text → {“role”: “This message’s role”, “content”: “This message’s content”}
Delimiter → ,

assuming, your message table has role and content as its attributes.

This way, the list of items, get formatted as a single text in the above format with individual items separated by a comma.

Let me know if this makes sense :slight_smile:

Thank you so much for your reply!

So the final json body would look like this? Do I want to format as text or as json? I included a screenshot of how our database is structured now

{
“model”: “claude-3-5-sonnet-20240320”,
“max_tokens”: 1024,
“system”: “You are a helpful AI assistant.”,
“messages”: If(ThreadExists,
Do a search for Messages:
Filter by thread equals :threadId
Sort by Created Date ascending
format as json
{“role”: “This Message’s from who”, “content”: “This Message’s text itself”},
[{“role”: “user”, “content”: “Initial user message”}]
)
}

You should use the format as text operator
I don’t think Bubble has a format as JSON operator atm. It has JSON-safe but it sometimes breaks for no reason.

In order to put “do a search for messages” within the API call, do I need to do any special punctuation? Or do I need to add it as a workflow in bubble? Or will it just work as you outlined?

Nothing specific. If you are calling it within your workflow action of the API call, it should be fine. But ofc, let me know if you need any help :slight_smile:

Are you still trying to figure this out? I’m currently testing, but i think i’ve figured out an easy way to get this done. lmk

“content”: This message’s content:formatted as JSON-safe

should be used as else if the content contains quotation marks or newlines the JSON will break

1 Like

Good call - but I’ve noticed that formatted as JSON-safe often doesn’t work. I might finaaally find time this weekend to raise a bug report with Bubble though

In what case? I’ve never had an issue with it

  • for a non-empty string, it will make JSON-safe the string and surround it in quotation marks
  • for an empty string, it will return just the two quotation marks so the JSON you’re using it in remains valid

Empty string is fine, but in a non-empty string, it doesn’t escape special characters properly, which ultimately results in an error at the API connector level

:format as JSON safe is not the problem. Your issues lies with :format as text where newlines will break JSON formatting.

So, suppose you have an array to use in API connector such as [Search:format as text]

Inside format as text, you have some JSON like {“key1”: This Thing’s X:formatted as JSON safe, “key2”: This Thing’s Y:formatted as JSON safe}. This will work fine.

However, if you added a new line between key 1 and key 2, as you might if you ran it through a JSON ‘beautifier, that’ll break the JSON formatting, because the newline is not JSON safe.

Yes, this is exactly what I’m referring to

However, this, up until a few months back, used to fail in some instances which is why I had to use reg-ex and/or find and replace which wasn’t convenient. Idk if they have made any changes though, will have to check

It’s the expected behaviour - just format inside format as text without newlines

So,

    {
      "id": 1,
      "title": "To Kill a Mockingbird",
      "author": "Harper Lee",
      "year": 1960,
      "genres": ["Fiction", "Coming-of-age"]
    }

should become:

{"id":1,"title":"To Kill a Mockingbird","author":"Harper Lee","year":1960,"genres":["Fiction","Coming-of-age"]}

This will work every time :slight_smile:

1 Like

Exactly. That’s what i’ve been experimenting with. Adding the formatted as JSON-safe inside the formatted as text was the key. Thanks for sharing.