OpenAI 从提示词对象迁移
OpenAI正在弃用API中的可复用prompt对象。自2026年6月3日起,产品逐步弱化创建prompt对象的入口,v1/prompts计划于2026年11月30日关闭。当前时间表见弃用记录。
要从OpenAI API平台的Prompts迁出,应将托管prompt对象中的内容移入应用代码,以更好地控制审核、测试、部署和版本管理。
迁移前:使用prompt对象
使用prompt对象:
来源示例 1(javascript):
代码内提示词/文本译释:
billing question:账单咨询。
// [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
prompt: {
id: "pmpt_123",
version: "1",
variables: {
customer_name: "Acme",
issue: "billing question",
},
},
});
来源示例 2(python):
代码内提示词/文本译释:
billing question:账单咨询。
## [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
## Replace the illustrative IDs and URLs below with your own resource values.
from openai import OpenAI
client = OpenAI()
prompt_id = "pmpt_123"
response = client.responses.create(
prompt={
"id": prompt_id,
"version": "1",
"variables": {
"customer_name": "Acme",
"issue": "billing question",
},
}
)
来源示例 3(go):
代码内提示词/文本译释:
billing question:账单咨询。
// [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Prompt: responses.ResponsePromptParam{
// Replace with your published prompt ID.
ID: "pmpt_123",
Version: openai.String("1"),
Variables: map[string]responses.ResponsePromptVariableUnionParam{
"customer_name": {OfString: openai.String("Acme")},
"issue": {OfString: openai.String("billing question")},
},
},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
来源示例 4(java):
代码内提示词/文本译释:
billing question:账单咨询。
// [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonValue;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponsePrompt;
String promptId = "pmpt_123";
ResponseCreateParams params =
ResponseCreateParams.builder()
.prompt(
ResponsePrompt.builder()
.id(promptId)
.version("1")
.variables(
ResponsePrompt.Variables.builder()
.putAdditionalProperty("customer_name", JsonValue.from("Acme"))
.putAdditionalProperty("issue", JsonValue.from("billing question"))
.build())
.build())
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
来源示例 5(ruby):
代码内提示词/文本译释:
billing question:账单咨询。
## [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
require "openai"
client = OpenAI::Client.new
response = client.responses.create(
prompt: {
id: "pmpt_123",
version: "1",
variables: {
customer_name: "Acme",
issue: "billing question"
}
}
)
puts(response.output_text)
来源示例 6(bash):
代码内提示词/文本译释:
billing question:账单咨询。
## [中文] 迁移前:用托管prompt对象的ID、版本和变量发起Responses请求;当前官方 Python 与其余 SDK 示例使用 id,curl 示例仍使用 prompt_id,字段口径差异待核,不据此确认当前请求有效。
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"prompt": {
"prompt_id": "pmpt_123",
"version": "1",
"variables": {
"customer_name": "Acme",
"issue": "billing question"
}
}
}'
迁移后:将提示词直接放入代码
在代码中内联提示词:
来源示例 7(javascript):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
// [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-6-astra",
input: [
{
role: "system",
content:
"You are a helpful support assistant. Be concise, accurate, and friendly.",
},
{
role: "user",
content:
"Customer name: Acme. Issue: billing question. Write a response to the customer.",
},
],
});
console.log(response.output_text);
来源示例 8(python):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
## [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-6-astra",
input=[
{
"role": "system",
"content": "You are a helpful support assistant. Be concise, accurate, and friendly.",
},
{
"role": "user",
"content": "Customer name: Acme. Issue: billing question. Write a response to the customer.",
},
],
)
print(response.output_text)
来源示例 9(go):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
// [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{OfInputItemList: responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage("You are a helpful support assistant. Be concise, accurate, and friendly.", responses.EasyInputMessageRoleSystem),
responses.ResponseInputItemParamOfMessage("Customer name: Acme. Issue: billing question. Write a response to the customer.", responses.EasyInputMessageRoleUser),
}},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
来源示例 10(java):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
// [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.EasyInputMessage;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputItem;
import java.util.List;
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.inputOfResponse(
List.of(
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.SYSTEM)
.content(
"You are a helpful support assistant. Be concise, accurate, and friendly.")
.build()),
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.USER)
.content(
"Customer name: Acme. Issue: billing question. Write a response to the customer.")
.build())))
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
来源示例 11(csharp):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
// [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
[
ResponseItem.CreateSystemMessageItem(
"You are a helpful support assistant. Be concise, accurate, and friendly."
),
ResponseItem.CreateUserMessageItem(
"Customer name: Acme. Issue: billing question. Write a response to the customer."
),
]
);
Console.WriteLine(response.GetOutputText());
来源示例 12(ruby):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
## [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
require "openai"
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: [
{
role: :system,
content: "You are a helpful support assistant. Be concise, accurate, and friendly."
},
{
role: :user,
content: "Customer name: Acme. Issue: billing question. Write a response to the customer."
}
]
)
puts(response.output_text)
来源示例 13(bash):
代码内提示词/文本译释:
Customer name: Acme. Issue: billing question. Write a response to the customer.:客户名称:Acme。问题:账单咨询。请给客户写一段回复。You are a helpful support assistant. Be concise, accurate, and friendly.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答。
## [中文] 迁移后:将客服系统指令和用户内容直接组成input消息,用明确model调用Responses,不再引用托管prompt对象。
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-6-astra",
"input": [
{
"role": "system",
"content": "You are a helpful support assistant. Be concise, accurate, and friendly."
},
{
"role": "user",
"content": "Customer name: Acme. Issue: billing question. Write a response to the customer."
}
]
}'
使用Codex迁移
可以结合OpenAI Developers插件与OpenAI Docs技能,自动处理迁移,并加快OpenAI API应用开发。
来源示例 14(text):
中文译释:供粘贴到Codex的提示词完整译文:
$openai-docs 更新此项目,将提示词保存在代码中,不再使用prompt对象。
这里是技能调用话术,不是本次制作需要执行的命令。
$openai-docs update this project to store prompts in code instead of using a prompts object
有哪些变化
不再在API请求中引用已保存的prompt对象,而是将提示词文本保存在代码库中,生成消息后直接通过Responses调用的input传入。
- 将提示词内容移入源代码:使其变更与产品逻辑使用相同审核和发布流程。
- 以函数参数代替prompt变量:让动态值在应用中显式表示并具有类型。
- 通过
input传入消息:Responses请求不再使用prompt对象。 - 将版本管理移入代码仓库:使用git提交、PR审核、测试或评测。
- 静态内容放前面,动态内容放后面:缓存命中依赖完全相同的前缀,以此保留提示缓存收益。
示例
通过辅助函数构建提示词:
来源示例 15(javascript):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。Customer name: ${customerName}. Issue: ${issue}. Write a response to the customer.:客户名称:${customerName}。问题:${issue}。请给客户写一段回复。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
// [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
import OpenAI from "openai";
const client = new OpenAI();
function buildSupportPrompt({ customerName, issue }) {
return [
{
role: "system",
content:
"You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.",
},
{
role: "user",
content: `Customer name: ${customerName}. Issue: ${issue}. Write a response to the customer.`,
},
];
}
const response = await client.responses.create({
model: "gpt-6-astra",
input: buildSupportPrompt({
customerName: "Acme",
issue: "billing question",
}),
});
来源示例 16(python):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。Customer name: {customer_name}. Issue: {issue}. Write a response to the customer.:客户名称:{customer_name}。问题:{issue}。请给客户写一段回复。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
## [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
from openai import OpenAI
client = OpenAI()
def build_support_prompt(customer_name, issue):
return [
{
"role": "system",
"content": "You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.",
},
{
"role": "user",
"content": f"Customer name: {customer_name}. Issue: {issue}. Write a response to the customer.",
},
]
response = client.responses.create(
model="gpt-6-astra",
input=build_support_prompt(
customer_name="Acme",
issue="billing question",
),
)
来源示例 17(go):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
// [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient()
response, err := client.Responses.New(context.Background(), responses.ResponseNewParams{
Model: "gpt-6-astra",
Input: responses.ResponseNewParamsInputUnion{OfInputItemList: buildSupportPrompt("Acme", "billing question")},
})
if err != nil {
panic(err)
}
fmt.Println(response.OutputText())
}
func buildSupportPrompt(customerName string, issue string) responses.ResponseInputParam {
return responses.ResponseInputParam{
responses.ResponseInputItemParamOfMessage("You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.", responses.EasyInputMessageRoleSystem),
responses.ResponseInputItemParamOfMessage(fmt.Sprintf("Customer name: %s. Issue: %s. Write a response to the customer.", customerName, issue), responses.EasyInputMessageRoleUser),
}
}
来源示例 18(java):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
// [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.EasyInputMessage;
import com.openai.models.responses.ResponseCreateParams;
import com.openai.models.responses.ResponseInputItem;
import java.util.List;
private static List<ResponseInputItem> buildSupportPrompt(String customerName, String issue) {
return List.of(
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.SYSTEM)
.content(
"You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.")
.build()),
ResponseInputItem.ofEasyInputMessage(
EasyInputMessage.builder()
.role(EasyInputMessage.Role.USER)
.content(
"Customer name: "
+ customerName
+ ". Issue: "
+ issue
+ ". Write a response to the customer.")
.build()));
}
ResponseCreateParams params =
ResponseCreateParams.builder()
.model("gpt-6-astra")
.inputOfResponse(buildSupportPrompt("Acme", "billing question"))
.build();
client.responses().create(params).output().stream()
.flatMap(item -> item.message().stream())
.flatMap(message -> message.content().stream())
.flatMap(content -> content.outputText().stream())
.forEach(text -> System.out.println(text.text()));
来源示例 19(csharp):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
// [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
using OpenAI.Responses;
#pragma warning disable OPENAI001
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
ResponsesClient client = new(key);
static ResponseItem[] BuildSupportPrompt(string customerName, string issue) =>
[
ResponseItem.CreateSystemMessageItem(
"You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details."
),
ResponseItem.CreateUserMessageItem(
$"Customer name: {customerName}. Issue: {issue}. Write a response to the customer."
),
];
ResponseResult response = await client.CreateResponseAsync(
"gpt-6-astra",
BuildSupportPrompt("Acme", "billing question")
);
Console.WriteLine(response.GetOutputText());
来源示例 20(ruby):
代码内提示词/文本译释:
You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details.:你是一位乐于助人的客服助手。请简洁、准确、友好地回答,不要编造政策细节。billing question:账单咨询。
动态消息模板译文:客户名称为传入的customer_name(或customerName),问题为issue;请给客户写一段回复。模板的语言插值语法保持原样。
## [中文] 将提示词封装为具名构建函数,customer_name和issue作为参数传入,再将消息列表交给Responses;静态指导在前,动态客户内容在后。
require "openai"
def build_support_prompt(customer_name, issue)
[
{
role: :system,
content: "You are a helpful support assistant. Be concise, accurate, and friendly. Do not invent policy details."
},
{
role: :user,
content: "Customer name: #{customer_name}. Issue: #{issue}. Write a response to the customer."
}
]
end
client = OpenAI::Client.new
response = client.responses.create(
model: "gpt-6-astra",
input: build_support_prompt("Acme", "billing question")
)
puts(response.output_text)
可以获得什么
工程控制更集中:提示词与产品代码共同维护,变更经过PR审核,可在CI运行测试与评测,并通过自己的配置或功能开关管理发布和实验。
不要把提示词零散分布在整个代码库中。创建小型prompts/模块,为每个提示词提供具名构建函数,并加入轻量评测样本,使提示词变更像产品逻辑一样受到审核。

TRAE-AI编程


更多建议: