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 characters in message contents result in request bodies that cause 400s returned from APIs #42

Open
preston-m-price opened this issue Oct 24, 2024 · 0 comments

Comments

@preston-m-price
Copy link

Consider these messageParams:

messageParams := []anthropic.MessageParam{
  anthropic.NewUserMessage(anthropic.NewTextBlock("What is this control character: \u0002?")),
}

// invoke with streaming, or non-streaming and get a 400 back.

This results in a JSON payload like:

{\"messages\":[{\"content\":[{\"text\":\"What is this control character: \\x02.\",\"type\":\"text\"}],\"role\":\"user\"}]}

Which results in a 400 returned by Anthropic's APIs.

This JSON cannot be parsed in all cases, for example:

anthropicBody := "{\"messages\":[{\"content\":[{\"text\":\"What is this control character: \\x02.\",\"type\":\"text\"}],\"role\":\"user\"}]}"
var data map[string]interface{}
err := json.Unmarshal([]byte(anthropicBody), &data)

results in an error:

invalid character 'x' in string escape code

When testing other model SDKs the resulting JSON payload for a message like this is of the form:

{\"messages\":[{\"content\":\"What is this control character: \\u0002.\",\"role\":\"user\"}]}

Which can be parsed using the strategy above.

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