Liner breaker and error 400

Hello everybody! I’m trying to add a line break to my prompt, but I’m having some issues.

The following request:

{
“messages”: [
{“role”: “user”, “content”: “My question with \n line breaker”}
]
} doesn’t work. I receive a Server returned HTTP response code: 400 for URL: https://api.openai.com/v1/chat/completions error.

However, this request:
{
“messages”: [
{“role”: “user”, “content”: “My question with no line breaker”}
]
} works fine.

I found this topic ChatGPT API - messages with line breaks but none of the solutions worked for me. Any assistance would be really appreciated.

Hi!

It probably depends on what environment you’re using. Different programming languages and shells and parsers and things treat strings differently, and it’s possible that using \n breaks the json call something somewhere for you.

you can try \\n, but it’s hard to say without more information; how you’re running this.

2 Likes

Unfortunately \\n doesn’t work either.
I’m in a Java environment and i’m using gpt-4-1106-preview model.
I’m new to the subjet, can you tell me what information should I give ?

Ah. Have you considered using a string builder or concatenation or something for your request? then you can construct your content with a multiline string or something so you don’t have to worry about that stuff.

I would also encourage you to dump or print your final constructed request before you send it out, to ensure that you didn’t accidentally mangle it somehow.

there is nothing really prohibiting newlines; the resulting request just needs to be a properly formatted json.

and the error you’re getting says exactly that: 400: bad request - you mangled your json.

Given that, you could consider using jackson or something to build the json for you :thinking:

or, read on and consider @PaulBellow 's post :laughing:

2 Likes

Can also check out the Java library…

Java

Is it only spitting out 400 error no text?

Can we see the rest of your connect code?

Might be not sending verification right, but 400 is a less common error not in API docs that I found…

https://help.openai.com/en/collections/3808446-api-error-codes-explained

Maybe it’s not even connecting? More code/details would help.

2 Likes

I stopped using the openai-java library because the main contributor desn’t seem to be around anymore and we had a few problems earlier this year. So I’m doing everything from scratch now :smiling_face_with_tear:

So here’s my code :

            URL obj = new URL("https://api.openai.com/v1/chat/completions");
            HttpURLConnection connection = (HttpURLConnection) obj.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Authorization", "Bearer " + this.apiKey);
            connection.setRequestProperty("Content-Type", "application/json");

            StringBuilder bodyBuilder = new StringBuilder();
            bodyBuilder.append("{\"model\": \"").append(model).append("\", \"messages\": [{\"role\": \"user\", \"content\": \"").append("Question with \n line breaker").append("\"}]}");

            String body = bodyBuilder.toString();
            System.out.println("BODY :: " +body);

            connection.setDoOutput(true);
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(body);
            writer.flush();
            writer.close();

            // Response from ChatGPT
            BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

And my print is :

We can see right after than I have an error. I don’t have any text that can give more informations about it.

If i change “Question with \n line breaker” to “Question with line breaker”, everything works :

1 Like

Yep

it’s tough. you likely need to replace/escape your newlines or add a line continuing character.

for escaping I don’t remember if it’s \\n or \\\\n. I think jackson would do it for you, but I would try one of these and check what your "BODY :: " output would say.

I haven’t touched java in quite a while and hope to never have to again :rofl:

1 Like

You saved me ! Thanks a lot !

“Question with \\\\n line breaker” is working fine :grin:

2 Likes