Different output when using \n for linebreaks

I’m using PlayGround webpage to test out different prompts, then I code them using the API. I noticed that ChatGPT gives different results when I replace line breaks with \n.

For my usecase, the system prompt is asking ChatGPT to provide yes/no based on the input. And the user prompt offers input, which is a chat conversation like this:

Man: Hi
Woman: Hi

However, if now I replace linebreaks with \n (also, what is provided using API),
Man: Hi\nWoman: Hi, ChatGPT gives me the opposite answer (yes → no), even with temperature and top_p set to 0.

Isn’t line break the same as “\n” to GhatGPT? I’m using gpt-3.5-turbo-0613.

Huh, I noticed the same thing. This is strange.

When using the API with client library I use \n to represent multiline prompts. The response made sense but was wrong semantically.

I tried this in the playground with actual newlines (like by pressing enter), and then with a block of text with \n. The former gives me the right response, the latter gives me the same wrong response as the API.

What’s the right way to represent new lines in the prompt when using the API?

Okay I figured it out, both in OpenAI Playgrounds and my own API calls. I inspected the network request payload on the Playground.

The backslash itself is escaped in the payload so what actually goes to the API is “\\n”, not a real encoded newline.

For example: “Hello \n\n World” turns into “Hello \\n\\n World” with an actual backslash character and an actual n character. This probably means that in the Playground, you need to use the ‘Enter key type encoded’ newline and not use \n.

My API call had the same issue, just needed to make sure \n was being escaped properly.

I somehow made it work, but I forgot what the difference was. (I remember it was related to how \n is represented.)

However, here’s how I compare the prompt I used in API to the Playground request. In Chrome, open the developer mode > Network, submit your prompt, and search for api.openai.com/v1/chat/completions. You’ll get the API call with the Request payload information. There you can copy the messages str.