Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Control Escape characters cause a 400 Bad Request #141

Open
rmalde opened this issue Dec 14, 2024 · 0 comments
Open

Control Escape characters cause a 400 Bad Request #141

rmalde opened this issue Dec 14, 2024 · 0 comments

Comments

@rmalde
Copy link

rmalde commented Dec 14, 2024

Control escape characters (like u+001b) sent in the request cause a 400 bad request error.

package main

import (
	"context"

	"github.com/openai/openai-go"
)

func main() {
	client := openai.NewClient()
	model := "gpt-4o-mini"
	ctx := context.Background()
	messages := []openai.ChatCompletionMessageParamUnion{
		openai.SystemMessage("You are an AI model"),
		openai.UserMessage("Hello\x1BWorld"),
	}

	params := openai.ChatCompletionNewParams{
		Messages:            openai.F(messages),
		Model:               openai.String(model),
	}

	chatCompletion, err := client.Chat.Completions.New(ctx, params)
	if err != nil {
		panic(err.Error())
	}
	println(chatCompletion.Choices[0].Message.Content)
}
panic: 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
            }
        }

This is handled gracefully on the sashabaranov/go-openai library

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant