Skip to content

Commit e0b46c4

Browse files
authored
fix: #425 improve the compatibility for conversationId / previousResponseId + tool calls (#556)
1 parent 0a0a984 commit e0b46c4

File tree

8 files changed

+1124
-44
lines changed

8 files changed

+1124
-44
lines changed

.changeset/heavy-foxes-sit.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@openai/agents-core': patch
3+
---
4+
5+
fix: improve the compatibility for conversationId / previousResponseId + tool calls
6+
7+
ref: https://github.com/openai/openai-agents-python/pull/1827

docs/src/content/docs/guides/running-agents.mdx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import helloWorldWithRunnerExample from '../../../../../examples/docs/hello-worl
88
import helloWorldExample from '../../../../../examples/docs/hello-world.ts?raw';
99
import runningAgentsExceptionExample from '../../../../../examples/docs/running-agents/exceptions1.ts?raw';
1010
import chatLoopExample from '../../../../../examples/docs/running-agents/chatLoop.ts?raw';
11+
import conversationIdExample from '../../../../../examples/docs/running-agents/conversationId.ts?raw';
12+
import previousResponseIdExample from '../../../../../examples/docs/running-agents/previousResponseId.ts?raw';
1113

1214
Agents do nothing by themselves – you **run** them with the `Runner` class or the `run()` utility.
1315

@@ -95,6 +97,32 @@ Each call to `runner.run()` (or `run()` utility) represents one **turn** in your
9597

9698
See [the chat example](https://github.com/openai/openai-agents-js/tree/main/examples/basic/chat.ts) for an interactive version.
9799

100+
### Server-managed conversations
101+
102+
You can let the OpenAI Responses API persist conversation history for you instead of sending your entire local transcript on every turn. This is useful when you are coordinating long conversations or multiple services. See the [Conversation state guide](https://platform.openai.com/docs/guides/conversation-state?api-mode=responses) for details.
103+
104+
OpenAI exposes two ways to reuse server-side state:
105+
106+
#### 1. `conversationId` for an entire conversation
107+
108+
You can create a conversation once using [Conversations API](https://platform.openai.com/docs/api-reference/conversations/create) and then reuse its ID for every turn. The SDK automatically includes only the newly generated items.
109+
110+
<Code
111+
lang="typescript"
112+
code={conversationIdExample}
113+
title="Reusing a server conversation"
114+
/>
115+
116+
#### 2. `previousResponseId` to continue from the last turn
117+
118+
If you want to start only with Responses API anyway, you can chain each request using the ID returned from the previous response. This keeps the context alive across turns without creating a full conversation resource.
119+
120+
<Code
121+
lang="typescript"
122+
code={previousResponseIdExample}
123+
title="Chaining with previousResponseId"
124+
/>
125+
98126
## Exceptions
99127

100128
The SDK throws a small set of errors you can catch:

examples/basic/conversations.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Agent, run } from '@openai/agents';
1+
import { Agent, run, tool } from '@openai/agents';
22
import OpenAI from 'openai';
3+
import z from 'zod';
34

45
async function main() {
56
const client = new OpenAI();
@@ -9,22 +10,47 @@ async function main() {
910
console.log(`New conversation: ${JSON.stringify(newConvo, null, 2)}`);
1011
const conversationId = newConvo.id;
1112

13+
const getWeatherTool = tool({
14+
name: 'get_weather',
15+
description: 'Get the weather for a given city',
16+
parameters: z.object({ city: z.string() }),
17+
strict: true,
18+
async execute({ city }) {
19+
return `The weather in ${city} is sunny.`;
20+
},
21+
});
22+
1223
const agent = new Agent({
1324
name: 'Assistant',
1425
instructions: 'You are a helpful assistant. be VERY concise.',
26+
tools: [getWeatherTool],
1527
});
1628

1729
// Set the conversation ID for the runs
1830
console.log('\n### Agent runs:\n');
19-
const runOptions = { conversationId };
31+
const options = { conversationId };
2032
let result = await run(
2133
agent,
2234
'What is the largest country in South America?',
23-
runOptions,
35+
options,
36+
);
37+
// First run: The largest country in South America is Brazil.
38+
console.log(`First run: ${result.finalOutput}`);
39+
result = await run(agent, 'What is the capital of that country?', options);
40+
// Second run: The capital of Brazil is Brasília.
41+
console.log(`Second run: ${result.finalOutput}`);
42+
43+
result = await run(agent, 'What is the weather in the city today?', options);
44+
// Thrid run: The weather in Brasília today is sunny.
45+
console.log(`Thrid run: ${result.finalOutput}`);
46+
47+
result = await run(
48+
agent,
49+
`Can you share the same information about the smallest country's capital in South America?`,
50+
options,
2451
);
25-
console.log(`First run: ${result.finalOutput}`); // e.g., Brazil
26-
result = await run(agent, 'What is the capital of that country?', runOptions);
27-
console.log(`Second run: ${result.finalOutput}`); // e.g., Brasilia
52+
// Fourth run: The smallest country in South America is Suriname. Its capital is Paramaribo. The weather in Paramaribo today is sunny.
53+
console.log(`Fourth run: ${result.finalOutput}`);
2854

2955
console.log('\n### Conversation items:\n');
3056
const convo = await client.conversations.items.list(conversationId);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Agent, run } from '@openai/agents';
2+
import { OpenAI } from 'openai';
3+
4+
const agent = new Agent({
5+
name: 'Assistant',
6+
instructions: 'Reply very concisely.',
7+
});
8+
9+
async function main() {
10+
// Create a server-managed conversation:
11+
const client = new OpenAI();
12+
const { id: conversationId } = await client.conversations.create({});
13+
14+
const first = await run(agent, 'What city is the Golden Gate Bridge in?', {
15+
conversationId,
16+
});
17+
console.log(first.finalOutput);
18+
// -> "San Francisco"
19+
20+
const second = await run(agent, 'What state is it in?', { conversationId });
21+
console.log(second.finalOutput);
22+
// -> "California"
23+
}
24+
25+
main().catch(console.error);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Agent, run } from '@openai/agents';
2+
3+
const agent = new Agent({
4+
name: 'Assistant',
5+
instructions: 'Reply very concisely.',
6+
});
7+
8+
async function main() {
9+
const first = await run(agent, 'What city is the Golden Gate Bridge in?');
10+
console.log(first.finalOutput);
11+
// -> "San Francisco"
12+
13+
const previousResponseId = first.lastResponseId;
14+
const second = await run(agent, 'What state is it in?', {
15+
previousResponseId,
16+
});
17+
console.log(second.finalOutput);
18+
// -> "California"
19+
}
20+
21+
main().catch(console.error);

0 commit comments

Comments
 (0)