openai-go icon indicating copy to clipboard operation
openai-go copied to clipboard

ChatCompletion request fails with 400 error when prompt contains special characters β€” invalid JSON not properly handled

Open mudassirnizamani opened this issue 6 months ago β€’ 0 comments

πŸ› Bug: ChatCompletion fails with 400 error when prompt contains special characters

Description

When using the ChatCompletion function from the openai_go library, requests fail with a 400 Bad Request error if the input prompt contains special characters (e.g., quotes, newlines, backslashes, non-ASCII symbols).

The error returned by the OpenAI API indicates that the JSON body of the request is malformed.

This should not occur, as the OpenAI API supports prompts with special characters. This suggests the library is either failing to properly escape or encode the request body as valid JSON, or it’s incorrectly formatting the request.


πŸ§ͺ Error Example

POST "https://api.openai.com/v1/chat/completions": 400 Bad Request { "error": { "message": "We could not parse the JSON body of your request. (HINT: This likely means you aren't using your HTTP library correctly. The OpenAI API expects a JSON payload, but what was sent was not valid JSON. If you have trouble figuring out how to fix this, please contact us through our help center at help.openai.com.)", "type": "invalid_request_error", "param": null, "code": null } }


πŸ” Steps to Reproduce

  1. Create a prompt that includes special characters, such as:

    prompt := "Tell me a story about \"Alice\" & her adventures in <Wonderland>.\nMake it fun & mysterious!"
    
  2. Pass it to the ChatCompletion call:

    resp, err := client.ChatCompletion(openai.ChatCompletionRequest{
        Model: "gpt-4",
        Messages: []openai.ChatMessage{
            {Role: "user", Content: prompt},
        },
    })
    
  3. Observe that the request fails with a 400 error.


βœ… Expected Behavior

  • The library should internally escape or encode special characters to ensure the resulting JSON is valid.
  • The OpenAI API should accept the prompt without error, since the content itself is valid for the LLM.

❌ Actual Behavior

  • Request fails due to an invalid JSON payload, indicating a serialization or encoding issue inside the library.

πŸ›  Suggested Fix

  • Ensure that the request body is being serialized using a proper JSON marshaller that escapes all special characters appropriately.
  • Add unit tests covering prompts with:
    • Quotes (")
    • Newlines (\n)
    • Unicode characters
    • Special symbols (<, >, &, \, etc.)

πŸ“Ž Additional Context

This bug makes it impossible to send natural language inputs with common punctuation or formatting, which is a core use case for LLMs. It should be handled gracefully by the library, not passed as malformed JSON.

mudassirnizamani avatar Jun 06 '25 07:06 mudassirnizamani