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

使用Azure OpenAI API传附文件消息时已含content仍报参数缺失

问题原因及解决方案

核心原因

你使用的Azure OpenAI API(2024-08-01-preview版本)要求**content字段必须是数组类型**,而非直接传入字符串。当前你将content设为字符串,不符合API的请求结构规范,导致API无法识别有效内容,从而抛出"Missing required parameter: 'content'"错误。

在该API版本中,content数组的每个元素需要明确指定内容类型(如text),结构示例为:

"content": [
  {
    "type": "text",
    "text": "Summarize the attached file."
  }
]

修改后的代码

调整messageBody中的content字段为数组格式即可解决问题:

var messageBody = new
{
    role = "user",
    content = new[] // 修改为数组结构
    {
        new { type = "text", text = "Summarize the attached file." }
    },
    attachments = new[]
    {
        new
        {
            file_id = fileId,
            tools = new[] { new { type = "file_search" } }
        }
    }
};

// 后续序列化、请求代码保持不变
var jsonMessageBody = JsonSerializer.Serialize(messageBody, new JsonSerializerOptions
{
    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    WriteIndented = true
});

Console.WriteLine($"JSON Payload: {jsonMessageBody}");

var messageContent = new StringContent(jsonMessageBody, Encoding.UTF8, "application/json");

var response = await client.PostAsync(
    $"{AzureEndpoint}/openai/threads/{threadId}/messages?api-version=2024-08-01-preview",
    messageContent
);

var messageJson = await response.Content.ReadAsStringAsync();
Console.WriteLine($"API Response: {messageJson}");

if (!response.IsSuccessStatusCode)
{
    throw new Exception($"Error sending message: {response.StatusCode} - {messageJson}");
}

验证要点

修改后序列化生成的JSON中,content会是符合要求的数组结构,API就能正确识别内容参数,不再返回缺失错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 06:42:04