Why does one invalid ChatMessage break all future API requests?

Hi everyone,

I’ve built a conversational AI chatbot in Bubble, where each user message is saved to a database and then used to generate conversational context for an API call.

I noticed a strange issue:

If a user submits an input that is invalid or doesn’t make sense (e.g. random characters like “DDD” or a misspelling that cannot be understood at all), the system returns an error (400 response from the API).

After this happens, even when I send a valid message/question afterwards, the system continues to fail and keeps returning the same error until I manually delete the problematic entries from the database.

And, in the database, the cell for the AI response after the first error stays empty.

It feels like the app is getting “stuck” on the bad record.

This is my setup:

A. Messages are stored in a ChatMessages table

B. API call uses Search for ChatMessages filtered by session_id and formatted as conversation history

C. Each new message is appended to the DB before or after the API call

Why would a single invalid message cause subsequent valid requests to keep failing?

Any guidance would be greatly appreciated.

It would be helpful to share a screenshot of your data types, workflows, and API connector request.

You’ve answered that here:

Any message a user sends is used in all subsequent API requests. The chances are that you’re not using :formatted as JSON safe and JSON breaking characters like quotation marks or newlines cause it to fail as the API call will no longer be valid JSON.

Thank you very much for your help!

I never leave any response unappreciated. But after this post, I got very busy with something else and completely stopped working on the App. So I forgot to respond.

I’m going to check & work on what you mentioned and see what happens. I’ll update here.

Thanks again!

The issue is most likely happening outside of Bubble.

And since you’re using the problematic message to generate the context, every subsequent call will keep resulting in errors.

Where are the messages being sent?

Directly to an LLM? To n8n? Make?

Thank you for your help!

Yes, that’s exactly what’s happening. I made some changes to it, but it’s still not resolved.

They are sent to Anthropic/ Claude.

It is an AI chatbot. I have trained it to answer specific questions and anything outside those to say something like “I’m sorry, I cannot answer xyz”. However, for some questions, when it gets stuck, it gives the same error message. No idea why! I’m reviewing the entire workflow again.

The solution is:

• Don’t write the assistant message until the API call succeeds. Put the “Create assistant ChatMessage” step after the call in the same workflow, using the returned text — so a failed turn never enters history.
• When you build the history, exclude anything invalid: add a constraint to the search like “content is not empty” (or :filtered) to drop messages with empty content / missing role before you format them into the messages array.
• Make sure every message you send has a valid role and non-empty content — that’s the actual thing the API validates.

Thank you so much for your help!

Your solution worked:) Much appreciated!