Skip to content

Amplify Gen 2 Bedrock AI generation task returning ValidationException (mapping error)

0

The exact code in the docs here: https://docs.amplify.aws/react-native/ai/generation/ for AI generation tasks in Amplify Gen 2 produces the following response:

{
  "data": {
    "generateRecipe": null
  },
  "errors": [
    {
      "path": [
        "generateRecipe"
      ],
      "data": null,
      "errorType": "ValidationException:http://internal.amazon.com/coral/com.amazon.bedrock/",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "A custom error was thrown from a mapping template."
    }
  ]
}

Exact code used to reproduce this in ./amplify/data/resource.ts:

const schema = a.schema({
...
  generateRecipe: a.generation({
    aiModel: a.ai.model('Amazon Nova Pro'),
    systemPrompt: 'You are a helpful assistant that generates recipes.',
  })
    .arguments({ description: a.string() })
    .returns(
      a.customType({
        name: a.string(),
        ingredients: a.string().array(),
        instructions: a.string(),
      })
    )
    .authorization((allow) => allow.authenticated()),
...
}).authorization((allow) => [
// just some lambda functions here
]);
export type Schema = ClientSchema<typeof schema>;

export const data = defineData({
  schema,
  authorizationModes: {
    defaultAuthorizationMode: "userPool",
    apiKeyAuthorizationMode: {
      expiresInDays: 30,
    },
  },
});

And the client side code to test is:

import { generateClient } from "aws-amplify/api";
import { createAIHooks } from "@aws-amplify/ui-react-ai";
import { Schema } from "../amplify/data/resource";
const client = generateClient<Schema>({ authMode: "userPool" });
const { useAIGeneration } = createAIHooks(client);
...
const [{ data, isLoading }, generateRecipe] = useAIGeneration("generateRecipe");

const generateSummary = async () => {
  await generateRecipe({
    description: 'I would like to bake a birthday cake for my friend. She has celiac disease and loves chocolate.',
  });
};

useEffect(() => console.log(isLoading, data), [isLoading, data]);
...
      <button onClick={() => generateSummary()}>click me!</button>

But this exact same error is produced when using the API playground off graphql queries like so:

query MyQuery {
  generateRecipe(description: "Chocolate cake") {
    ingredients
    instructions
    name
  }
}

I've attempted this with Claude Sonnet 3.5, Claude Sonnet 3.5 v2, Claude Haiku 3.5, DeepSeek-R1, and Amazon Nova Pro. Users are logged in, and this follows the exact code + guidelines from that aforementioned docs page. Any ideas?

2 Answers
0
Accepted Answer

The solution is to use the full proper model name (unlike how they are currently doing it in the documentation). After 10+ hours with support staff on this and purchasing an AWS support package it turns out the documentation misleads you about how to properly set this aiModel field on any of the AI models in Amplify schemas. The correct code is included below for a Claude Sonnet 3.5 v2 model:

  generateRecipe: a.generation({
    aiModel: {
      resourcePath: 'us.anthropic.claude-3-5-sonnet-20241022-v2:0'
    },
    systemPrompt: 'You are a helpful assistant that generates recipes.',
  })
    .arguments({ description: a.string() })
    .returns(
      a.customType({
        name: a.string(),
        ingredients: a.string().array(),
        instructions: a.string(),
      })
    )
    .authorization((allow) => allow.authenticated()),

Notice the difference in structure for the aiModel field. You can currently find those model instances listed here: https://docs.aws.amazon.com/bedrock/latest/userguide/models-supported.html

If you find within the "Regions supported" section for a proper model listed in the link above with an asterisk on your region you just apply a prefix like "us." to have your AI request processing done within the US (automatically at the fastest location based on traffic).

And you can find vague documentation on this matter and the different prefixes you must apply to some proper model names based on cross-region inference support here: https://docs.aws.amazon.com/bedrock/latest/userguide/inference-profiles-support.html

Update: you MAY have to also attach the AmazonBedrockFullAccess policy to the IAM role that runs the generation task! (or some other granular permission). Here's how I'm currently doing that, though know it's not the best way!

// at the bottom of ./amplify/backend.ts
// note: you ideally should not attach the 'AmazonBedrockFullAccess' policy to ALL of your IAM roles! your actual target function is like 'GenerationBedrockDat<small hash>'
Object.values(backend.data.resources.roles).forEach(role => {
  role.addManagedPolicy(
    iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonBedrockFullAccess')
  );
});
answered 7 months ago
AWS
EXPERT
reviewed 7 months ago
-1

The ValidationException error you're encountering is likely related to how you're using the Amazon Bedrock model in your Amplify Gen 2 AI generation task. There are a few potential causes for this issue:

  1. For custom fine-tuned models, you need to purchase Provisioned Throughput for models other than Amazon Nova and use the Provisioned Model ARN instead of the original model ARN. If you're trying to use the original model ARN directly in API calls, this could cause the validation error.

  2. The model ID format might not be correct. The error message suggests that the model ID is failing to satisfy a regular expression pattern constraint. Make sure you're using the correct model identifier format for Amazon Bedrock models.

  3. There might be an issue with the input format or parameters being passed to the model. The Bedrock API has specific constraints for inputs, and if these aren't met, a ValidationException is thrown.

  4. You might need specific IAM permissions to invoke the model you're trying to use. Ensure that your authenticated users have the necessary permissions to call the Bedrock service and the specific model.

To troubleshoot:

  • Verify that you're using the correct model identifier for Amazon Bedrock models
  • Check that your authenticated users have the proper IAM permissions to invoke the model
  • If using a fine-tuned model, ensure you're using the Provisioned Model ARN
  • Review the input format and parameters to ensure they meet the model's requirements
  • Consider checking CloudWatch logs for more detailed error information

If you're following the exact documentation but still encountering issues, it might be worth reaching out to AWS Support for more specific guidance related to your account configuration.
Sources
InvokeModelWithResponseStream - Amazon Bedrock
InvokeModel - Amazon Bedrock
Resolve errors when I fine-tune models on Amazon Bedrock | AWS re:Post

answered 7 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.