Validation Error in Assistant API type=value_error.jsondecode

I am trying to get Assistant API to compose an short summary essay from a input of about 10 paragraphs of text , each paragraph being a 20-30 words length. Not a whole lot of data perhaps

I randomly get this error sometimes.

1 validation error for Request body → 1103
Invalid control character at: line 1 column 1104 (char 1103) (type=value_error.jsondecode; msg=Invalid control character at; doc={ …

Has anyone faced this ? I am not able to figure out anything. If I shorten the text inputs at some point in time the error disappears - my total length is only 700 words - so it can be breaking GPT down I hope,

Well it turns out that the content that one feeds to createMessage (GET/v1/threads/thread_xxx/messages) is sensitive to presence of control characters like newline, tab, or double quotes - even if they are extracted from a valid json and escaped
The problem is CreateMessage API method may or may not throw an exception. Instead of throwing an exception it may return ajson of the following format

“error”: {
“message”: “1 validation error for Request\nbody → 42\n Expecting ‘,’ delimiter: line 1 column 43 (char 42) (type=value_error.jsondecode; msg=Expecting ‘,’ delimiter; doc={… }; pos=42; lineno=1; colno=43)”,
“type”: “invalid_request_error”,
“param”: null,
“code”: null

So the safe approach is to do both things after creating a message-

  1. check if returned json object has key “error”
  2. catch exception if thrown

This way you will be 100% sure that your messge has been created with the content you have provided and then you may proceed to call the RUN method