Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thin-plants-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@openai/agents-openai': patch
'@openai/agents-core': patch
---

#366 Add conversations API support
40 changes: 40 additions & 0 deletions examples/basic/conversations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Agent, run } from '@openai/agents';
import OpenAI from 'openai';

async function main() {
const client = new OpenAI();

console.log('### New conversation:\n');
const newConvo = await client.conversations.create({});
console.log(`New conversation: ${JSON.stringify(newConvo, null, 2)}`);
const conversationId = newConvo.id;

const agent = new Agent({
name: 'Assistant',
instructions: 'You are a helpful assistant. be VERY concise.',
});

// Set the conversation ID for the runs
console.log('\n### Agent runs:\n');
const runOptions = { conversationId };
let result = await run(
agent,
'What is the largest country in South America?',
runOptions,
);
console.log(`First run: ${result.finalOutput}`); // e.g., Brazil
result = await run(agent, 'What is the capital of that country?', runOptions);
console.log(`Second run: ${result.finalOutput}`); // e.g., Brasilia

console.log('\n### Conversation items:\n');
const convo = await client.conversations.items.list(conversationId);
for await (const page of convo.iterPages()) {
for (const item of page.getPaginatedItems()) {
// desc order
console.log(JSON.stringify(item, null, 2));
}
}
}
if (require.main === module) {
main().catch(console.error);
}
3 changes: 2 additions & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"start:tool-use-behavior": "tsx tool-use-behavior.ts",
"start:tools": "tsx tools.ts",
"start:reasoning": "tsx reasoning.ts",
"start:local-file": "tsx local-file.ts"
"start:local-file": "tsx local-file.ts",
"start:conversations": "tsx conversations.ts"
}
}
8 changes: 8 additions & 0 deletions packages/agents-core/src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ export type ModelRequest = {
*/
previousResponseId?: string;

/**
* The ID of stored conversation to use for the model.
*
* see https://platform.openai.com/docs/guides/conversation-state?api-mode=responses#openai-apis-for-conversation-state
* see https://platform.openai.com/docs/api-reference/conversations/create
*/
conversationId?: string;

/**
* The model settings to use for the model.
*/
Expand Down
3 changes: 3 additions & 0 deletions packages/agents-core/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type SharedRunOptions<TContext = undefined> = {
maxTurns?: number;
signal?: AbortSignal;
previousResponseId?: string;
conversationId?: string;
};

export type StreamRunOptions<TContext = undefined> =
Expand Down Expand Up @@ -393,6 +394,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
prompt: await state._currentAgent.getPrompt(state._context),
input: turnInput,
previousResponseId: options.previousResponseId,
conversationId: options.conversationId,
modelSettings,
tools: serializedTools,
outputType: convertAgentOutputTypeToSerializable(
Expand Down Expand Up @@ -757,6 +759,7 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
prompt: await currentAgent.getPrompt(result.state._context),
input: turnInput,
previousResponseId: options.previousResponseId,
conversationId: options.conversationId,
modelSettings,
tools: serializedTools,
handoffs: serializedHandoffs,
Expand Down
1 change: 1 addition & 0 deletions packages/agents-openai/src/openaiResponsesModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@ export class OpenAIResponsesModel implements Model {
include,
tools,
previous_response_id: request.previousResponseId,
conversation: request.conversationId,
prompt,
temperature: request.modelSettings.temperature,
top_p: request.modelSettings.topP,
Expand Down
Loading