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

Issues with sending requests containing certain characters #83

Open
cal-anthropic opened this issue Dec 19, 2024 · 5 comments
Open

Issues with sending requests containing certain characters #83

cal-anthropic opened this issue Dec 19, 2024 · 5 comments

Comments

@cal-anthropic
Copy link

Repro

package main

import (
	"context"
	"os"

	"github.com/anthropics/anthropic-sdk-go"
	"github.com/anthropics/anthropic-sdk-go/option"
)

func main() {
	client := anthropic.NewClient(
		option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")),
	)
	message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
		Model:     anthropic.F(anthropic.ModelClaude3_5SonnetLatest),
		MaxTokens: anthropic.F(int64(1024)),
		Messages: anthropic.F([]anthropic.MessageParam{
			anthropic.NewUserMessage(anthropic.NewTextBlock("What is a quaternion?\x1b")),
		}),
	})
	if err != nil {
		panic(err.Error())
	}
	println(message.Content[0].Text)
}

Output:

POST "https://api.anthropic.com/v1/messages": 400 Bad Request {"type":"error","error":{"type":"invalid_request_error","message":"The request body is not valid JSON: invalid escaped character in string: line 1 column 75 (char 74)"}}
@gdsoumya
Copy link

gdsoumya commented Jan 2, 2025

+1 having similar issues :

{\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"The request body is not valid JSON: invalid escaped character in string: line 1 column 1597 (char 1596)\"}}"

@jacobzim-stl
Copy link

Thanks! This is a known issue, we'll be releasing a v2 including a fix for this.

In the meantime, you should be able to bypass this issue via passing json.RawMessage into anthropic.Raw() instead of anthropic.Field() as the parameter.

@glamboyosa
Copy link

Thanks! This is a known issue, we'll be releasing a v2 including a fix for this.

In the meantime, you should be able to bypass this issue via passing json.RawMessage into anthropic.Raw() instead of anthropic.Field() as the parameter.

Hey, could you possibly provide a code example or reflect this in the README / docs? For example I tried: anthropic.Raw[]anthropic.MessageParam where messages are of type anthropic.MessageParam and no dice. tried marshalling messages myself no dice.

@glamboyosa
Copy link

Thanks! This is a known issue, we'll be releasing a v2 including a fix for this.
In the meantime, you should be able to bypass this issue via passing json.RawMessage into anthropic.Raw() instead of anthropic.Field() as the parameter.

Hey, could you possibly provide a code example or reflect this in the README / docs? For example I tried: anthropic.Raw[]anthropic.MessageParam where messages are of type anthropic.MessageParam and no dice. tried marshalling messages myself no dice.

For anyone who was stuck, here's how I got it to work:

messagesJson, _ := json.Marshal(messages)
message, err := client.Messages.New(context.TODO(), anthropic.MessageNewParams{
		Model:     anthropic.F(anthropic.ModelClaude3_5SonnetLatest),
		MaxTokens: anthropic.F(int64(1024)),
		Messages: anthropic.Raw[[]anthropic.MessageParam](json.RawMessage(messagesJson)),
		}),

In this example, messages is a slice of maps ([]map[string]string) that matches the official documentation, where each map contains "role" and "content" keys.

@zieen
Copy link

zieen commented Feb 20, 2025

any progress ?

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

5 participants