You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在.NET Core中配置Amazon Bedrock Claude函数调用遇验证错误求助

.NET Core中Amazon Bedrock Claude函数调用的正确配置方法

问题原因

你遇到的ValidationException是因为构建工具输入Schema时,使用了大写开头的Type属性,而Bedrock要求JSON Schema关键字必须是小写的type。C#匿名对象的PascalCase属性会被序列化为大写开头的键,不符合Bedrock的验证规则。

修正后的代码

方式一:使用Dictionary构建Schema(推荐)

通过Dictionary<string, object>确保JSON Schema关键字为小写:

var messages = new List<Message>
{
    new Message
    {
        Role = ConversationRole.User,
        Content = new List<ContentBlock> { new ContentBlock { Text = "Get the most popular song played on a radio station." } }
    }
};

// 构建符合JSON Schema规范的输入结构
var inputSchema = new Dictionary<string, object>
{
    ["type"] = "object",
    ["properties"] = new Dictionary<string, object>
    {
        ["sign"] = new Dictionary<string, object>
        {
            ["type"] = "string",
            ["description"] = "The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP."
        }
    },
    ["required"] = new List<string> { "sign" }
};

var response = await bedrock.ConverseAsync(new ConverseRequest
{
    ModelId = "anthropic.claude-3-sonnet-20240229-v1:0",
    Messages = messages,
    ToolConfig = new ToolConfiguration
    {
        Tools = new List<Tool>
        {
            new Tool
            {
                ToolSpec = new ToolSpecification
                {
                    Name = "GetSong",
                    Description = "Gets the current song on the radio",
                    InputSchema = new ToolInputSchema
                    {
                        Json = Amazon.Runtime.Documents.Document.FromObject(inputSchema)
                    }
                }
            }
        }
    }
});

方式二:直接解析JSON字符串

如果更习惯写原生JSON,可以直接解析字符串生成Document:

var messages = new List<Message>
{
    new Message
    {
        Role = ConversationRole.User,
        Content = new List<ContentBlock> { new ContentBlock { Text = "Get the most popular song played on a radio station." } }
    }
};

// 原生JSON Schema字符串
var inputSchemaJson = @"
{
    ""type"": ""object"",
    ""properties"": {
        ""sign"": {
            ""type"": ""string"",
            ""description"": ""The call sign for the radio station for which you want the most popular song. Example calls signs are WZPZ and WKRP.""
        }
    },
    ""required"": [""sign""]
}";

var response = await bedrock.ConverseAsync(new ConverseRequest
{
    ModelId = "anthropic.claude-3-sonnet-20240229-v1:0",
    Messages = messages,
    ToolConfig = new ToolConfiguration
    {
        Tools = new List<Tool>
        {
            new Tool
            {
                ToolSpec = new ToolSpecification
                {
                    Name = "GetSong",
                    Description = "Gets the current song on the radio",
                    InputSchema = new ToolInputSchema
                    {
                        Json = Amazon.Runtime.Documents.Document.Parse(inputSchemaJson)
                    }
                }
            }
        }
    }
});

关键注意事项

  • 所有JSON Schema关键字(type、properties、required)必须使用小写,严格遵循JSON Schema规范
  • 工具名称(GetSong)要与后续实际执行的函数名称完全一致
  • 确保required数组中的字段与properties定义的字段匹配

内容的提问来源于stack exchange,提问作者halllo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 16:25:18