fumito-ito / AnthropicSwiftSDK

Yet another Anthropic API client for Swift.
3 stars 1 forks source link

AWSBedrockRuntime.ValidationException when trying to invoke claude model #19

Closed hyperlink closed 2 months ago

hyperlink commented 3 months ago

AntropicSwiftSDK: 0.4.0 aws-sdk-swift: 0.46.0

I encountered a validation exception when sending a message using the Bedrock client. I plan on investigating further and updating this issue.

Test code:

let response = try await anthropic.messages.createMessage([AnthropicSwiftSDK.Message(role: .user, content: [.text("I'm testing this so please say 'I am working!'")])], maxTokens: 1024)

Error in the console:

ValidationException(properties: AWSBedrockRuntime.ValidationException.Properties(message: Optional("Malformed input request: #: subject must not be valid against schema {\"required\":[\"messages\"]}#: extraneous key [stream] is not permitted#: extraneous key [model] is not permitted, please reformat your input and try again.")), httpResponse: 
Status Code: http_status_400 
 x-amzn-errortype: ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/, 
x-amzn-requestid:  
Content-Length: 240, 
Content-Type: application/json, 
Date: Wed, 26 Jun 2024 03:36:00 GMT, message: Optional("Malformed input request: #: subject must not be valid against schema {\"required\":[\"messages\"]}#: extraneous key [stream] is not permitted#: extraneous key [model] is not permitted, please reformat your input and try again."), requestID: nil)
hyperlink commented 3 months ago

Looks like there are some extraneous fields in the body that bedrock does not like stream and model. If those keys are removed the invoke is sent successfully and the response is also handled correctly.

However, I encountered an issue using the streaming method. Looks like the response coming back from Bedrock is not in an EventStream format so it fails to parse the responses:

unknownStreamingResponseLine("{\"type\":\"message_start\",\"message\":{\"id\":\"msg_bdrk_014XMPhb8Q1dgobb29cjzVhj\",\"type\":\"message\",\"role\":\"assistant\",\"model\":\"claude-3-haiku-20240307\",\"stop_sequence\":null,\"usage\":{\"input_tokens\":20,\"output_tokens\":1},\"content\":[],\"stop_reason\":null}}")
fumito-ito commented 2 months ago

Hi @hyperlink . Thanks for your reporting.

The problems reported have been corrected. If you would like, could you try the main branch to see if the problem has been fixed?

fumito-ito commented 2 months ago

fixed by #20 and #21