Skip to content

fix(core): emit correct agent in agent_updated_stream_event during streaming handoff #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 14, 2025
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
5 changes: 5 additions & 0 deletions .changeset/twelve-poets-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@openai/agents-core': patch
---

Fixes handling of `agent_updated_stream_event` in run implementation and adds corresponding test coverage.
4 changes: 3 additions & 1 deletion packages/agents-core/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,9 @@ export class Runner extends RunHooks<any, AgentOutputType<unknown>> {
resetCurrentSpan();
}
result.state._currentAgentSpan = undefined;
result._addItem(new RunAgentUpdatedStreamEvent(currentAgent));
result._addItem(
new RunAgentUpdatedStreamEvent(result.state._currentAgent),
);
result.state._noActiveAgentRun = true;

// we've processed the handoff, so we need to run the loop again
Expand Down
74 changes: 73 additions & 1 deletion packages/agents-core/test/run.stream.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import {
run,
setDefaultModelProvider,
setTracingDisabled,
Usage,
RunStreamEvent,
RunAgentUpdatedStreamEvent,
handoff,
Model,
ModelRequest,
ModelResponse,
StreamEvent,
FunctionCallItem,
} from '../src';
import { FakeModel, FakeModelProvider } from './stubs';
import { FakeModel, FakeModelProvider, fakeModelMessage } from './stubs';

// Test for unhandled rejection when stream loop throws

Expand Down Expand Up @@ -43,4 +52,67 @@ describe('Runner.run (streaming)', () => {

expect((result.error as Error).message).toBe('Not implemented');
});

it('emits agent_updated_stream_event with new agent on handoff', async () => {
class SimpleStreamingModel implements Model {
constructor(private resp: ModelResponse) {}
async getResponse(_req: ModelRequest): Promise<ModelResponse> {
return this.resp;
}
async *getStreamedResponse(): AsyncIterable<StreamEvent> {
yield {
type: 'response_done',
response: {
id: 'r',
usage: {
requests: 1,
inputTokens: 0,
outputTokens: 0,
totalTokens: 0,
},
output: this.resp.output,
},
} as any;
}
}

const agentB = new Agent({
name: 'B',
model: new SimpleStreamingModel({
output: [fakeModelMessage('done B')],
usage: new Usage(),
}),
});

const callItem: FunctionCallItem = {
id: 'h1',
type: 'function_call',
name: handoff(agentB).toolName,
callId: 'c1',
status: 'completed',
arguments: '{}',
};

const agentA = new Agent({
name: 'A',
model: new SimpleStreamingModel({
output: [callItem],
usage: new Usage(),
}),
handoffs: [handoff(agentB)],
});

const result = await run(agentA, 'hi', { stream: true });
const events: RunStreamEvent[] = [];
for await (const e of result.toStream()) {
events.push(e);
}
await result.completed;

const update = events.find(
(e): e is RunAgentUpdatedStreamEvent =>
e.type === 'agent_updated_stream_event',
);
expect(update?.agent).toBe(agentB);
});
});