Skip to content

Commit 4f27ed5

Browse files
authored
fix: #558 prompt parameter does not work when being passed via an Agent (#559)
1 parent 42c2e47 commit 4f27ed5

File tree

5 files changed

+90
-9
lines changed

5 files changed

+90
-9
lines changed

.changeset/eleven-bananas-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@openai/agents-openai': patch
3+
---
4+
5+
fix: #558 prompt parameter does not work when being passed via an Agent

examples/basic/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"start:stream-text": "tsx stream-text.ts",
2525
"start:json-schema-output-type": "tsx json-schema-output-type.ts",
2626
"start:tool-use-behavior": "tsx tool-use-behavior.ts",
27+
"start:prompt-id": "tsx prompt-id.ts",
2728
"start:tools": "tsx tools.ts",
2829
"start:reasoning": "tsx reasoning.ts",
2930
"start:local-file": "tsx local-file.ts",

examples/basic/prompt-id.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ async function main() {
44
const agent = new Agent({
55
name: 'Assistant',
66
prompt: {
7-
promptId: 'pmpt_684b3b772e648193b92404d7d0101d8a07f7a7903e519946',
7+
promptId: 'pmpt_68d50b26524c81958c1425070180b5e10ab840669e470fc7',
88
version: '1',
9-
variables: {
10-
poem_style: 'limerick',
11-
},
9+
variables: { name: 'Kaz' },
1210
},
1311
});
1412

15-
const result = await run(agent, 'Write about unrequited love.');
13+
const result = await run(agent, 'What is your name?');
1614
console.log(result.finalOutput);
1715
}
1816

packages/agents-openai/src/openaiResponsesModel.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,8 @@ export class OpenAIResponsesModel implements Model {
878878
}
879879

880880
const requestData = {
881-
instructions: request.systemInstructions,
882881
model: this.#model,
882+
instructions: normalizeInstructions(request.systemInstructions),
883883
input,
884884
include,
885885
tools,
@@ -1051,3 +1051,21 @@ export class OpenAIResponsesModel implements Model {
10511051
}
10521052
}
10531053
}
1054+
1055+
/**
1056+
* Sending an empty string for instructions can override the prompt parameter.
1057+
* Thus, this method checks if the instructions is an empty string and returns undefined if it is.
1058+
* @param instructions - The instructions to normalize.
1059+
* @returns The normalized instructions.
1060+
*/
1061+
function normalizeInstructions(
1062+
instructions: string | undefined,
1063+
): string | undefined {
1064+
if (typeof instructions === 'string') {
1065+
if (instructions.trim() === '') {
1066+
return undefined;
1067+
}
1068+
return instructions;
1069+
}
1070+
return undefined;
1071+
}

packages/agents-openai/test/openaiResponsesModel.test.ts

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('OpenAIResponsesModel', () => {
1313
setTracingDisabled(true);
1414
});
1515
it('getResponse returns correct ModelResponse and calls client with right parameters', async () => {
16-
withTrace('test', async () => {
16+
await withTrace('test', async () => {
1717
const fakeResponse = {
1818
id: 'res1',
1919
usage: {
@@ -74,8 +74,67 @@ describe('OpenAIResponsesModel', () => {
7474
});
7575
});
7676

77+
it('normalizes systemInstructions so empty strings are omitted', async () => {
78+
await withTrace('test', async () => {
79+
const fakeResponse = {
80+
id: 'res-empty-instructions',
81+
usage: {
82+
input_tokens: 0,
83+
output_tokens: 0,
84+
total_tokens: 0,
85+
},
86+
output: [],
87+
};
88+
for (const systemInstructions of ['', ' ']) {
89+
const request = {
90+
systemInstructions,
91+
input: 'hello',
92+
modelSettings: {},
93+
tools: [],
94+
outputType: 'text',
95+
handoffs: [],
96+
tracing: false,
97+
signal: undefined,
98+
};
99+
const createMock = vi.fn().mockResolvedValue(fakeResponse);
100+
await new OpenAIResponsesModel(
101+
{ responses: { create: createMock } } as unknown as OpenAI,
102+
'gpt-test',
103+
).getResponse(request as any);
104+
105+
expect(createMock).toHaveBeenCalledTimes(1);
106+
const [args] = createMock.mock.calls[0];
107+
expect('instructions' in args).toBe(true);
108+
expect(args.instructions).toBeUndefined();
109+
}
110+
111+
for (const systemInstructions of [' a ', 'foo']) {
112+
const request = {
113+
systemInstructions,
114+
input: 'hello',
115+
modelSettings: {},
116+
tools: [],
117+
outputType: 'text',
118+
handoffs: [],
119+
tracing: false,
120+
signal: undefined,
121+
};
122+
const createMock = vi.fn().mockResolvedValue(fakeResponse);
123+
await new OpenAIResponsesModel(
124+
{ responses: { create: createMock } } as unknown as OpenAI,
125+
'gpt-test',
126+
).getResponse(request as any);
127+
128+
expect(createMock).toHaveBeenCalledTimes(1);
129+
const [args] = createMock.mock.calls[0];
130+
expect('instructions' in args).toBe(true);
131+
expect(args.instructions).toBe(systemInstructions);
132+
}
133+
});
134+
});
135+
77136
it('merges top-level reasoning and text settings into provider data for Responses API', async () => {
78-
withTrace('test', async () => {
137+
await withTrace('test', async () => {
79138
const fakeResponse = {
80139
id: 'res-settings',
81140
usage: {
@@ -134,7 +193,7 @@ describe('OpenAIResponsesModel', () => {
134193
});
135194

136195
it('getStreamedResponse yields events and calls client with stream flag', async () => {
137-
withTrace('test', async () => {
196+
await withTrace('test', async () => {
138197
const fakeResponse = { id: 'res2', usage: {}, output: [] };
139198
const events: ResponseStreamEvent[] = [
140199
{ type: 'response.created', response: fakeResponse as any },

0 commit comments

Comments
 (0)