vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
8.59k stars 1.21k forks source link

generateObject with AWS Bedrock (Claude 3.5 Sonnet) Fails with ValidationException #2166

Open mbokinala opened 4 days ago

mbokinala commented 4 days ago

Description

Calling generateObject() with the AWS Bedrock Claude 3.5 Sonnet model (bedrock("anthropic.claude-3-5-sonnet-20240620-v1:0")) causes the following error to occur:

/Users/mbokinala/projects/vly-v2/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1082
  const exception = new ValidationException({
                    ^

ValidationException: The format of the value at toolConfig.tools.0.toolSpec.inputSchema.json is invalid. Provide a json object for the field and try again.
    at de_ValidationExceptionRes (/Users/mbokinala/projects/vly-v2/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1082:21)
    at de_CommandError (/Users/mbokinala/projects/vly-v2/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:937:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async /Users/mbokinala/projects/vly-v2/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /Users/mbokinala/projects/vly-v2/node_modules/@smithy/core/dist-cjs/index.js:165:18
    at async /Users/mbokinala/projects/vly-v2/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38
    at async /Users/mbokinala/projects/vly-v2/node_modules/@aws-sdk/middleware-logger/dist-cjs/index.js:34:22
    at BedrockChatLanguageModel.doGenerate (/Users/mbokinala/projects/vly-v2/node_modules/@ai-sdk/amazon-bedrock/src/bedrock-chat-language-model.ts:159:22)
    at _retryWithExponentialBackoff (/Users/mbokinala/projects/vly-v2/node_modules/ai/core/util/retry-with-exponential-backoff.ts:36:12)
    at generateObject (/Users/mbokinala/projects/vly-v2/node_modules/ai/core/generate-object/generate-object.ts:178:30) {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 400,
    requestId: '51c84dca-51b0-4487-83b2-956b83fd61a5',
    extendedRequestId: undefined,
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  }
}

Node.js v18.19.1

Code example

(taken from the generateObject example)

(async () => {
  const { object } = await generateObject({
    model: bedrock("anthropic.claude-3-5-sonnet-20240620-v1:0"),
    schema: z.object({
      recipe: z.object({
        name: z.string(),
        ingredients: z.array(z.string()),
        steps: z.array(z.string()),
      }),
    }),
    prompt: "Generate a lasagna recipe.",
  });

  console.log(object);
})();

Additional context

This error does NOT occur when using the regular anthropic provider for the same model (anthropic("claude-3-5-sonnet-20240620")).

The error also does not occur when using Bedrock with generateText()

alexpate commented 4 days ago

I think there's something going on here.

Passing mode.tool.parameters without stringifying it makes the request work, but breaks types.

inputSchema: { json: mode.tool.parameters },
fahreziadh commented 1 day ago

I got the same issue when using stream Object or generate Object, using bedrock with model claude 3.5 sonnet

ValidationException: The format of the value at toolConfig.tools.0.toolSpec.inputSchema.json is invalid. Provide a json object for the field and try again.

then, i do this and everything went well : image