-
Notifications
You must be signed in to change notification settings - Fork 324
Open
Labels
Description
Describe the bug
When running the JS Agents SDK in streaming mode with an AbortController, aborting while the consumer is iterating the stream throws a runtime error: “TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is locked.” This happens because the SDK calls ReadableStream.cancel() while the stream is locked by an active async iterator.
Debug information
- Agents SDK version:
v0.1.3
- Runtime environment:
Node.js 22.10.0
Repro steps
Run the following script:
import { Agent, OpenAIProvider, Runner, setDefaultModelProvider, setTracingDisabled } from '@openai/agents'
setTracingDisabled(true)
setDefaultModelProvider(new OpenAIProvider())
function buildSimpleAgent() {
return new Agent({
name: 'Simple Chat Agent',
instructions: 'You are a helpful assistant. Respond with enough text to stream.',
model: 'openai/gpt-4o-mini',
tools: [],
})
}
async function main() {
const controller = new AbortController()
const agent = buildSimpleAgent()
const runner = new Runner()
const result = await runner.run(agent, 'Say something long to stream.', { stream: true, signal: controller.signal, maxTurns: 1 })
;(async () => { for await (const _ of result) {} })()
setTimeout(() => { controller.abort() }, 10)
}
main().catch(err => { console.error(err); process.exit(1) })
Observed error:
TypeError [ERR_INVALID_STATE]: Invalid state: ReadableStream is locked
at ReadableStream.cancel (node:internal/webstreams/readablestream:322:9)
at AbortSignal.<anonymous> (.../node_modules/@openai/agents-core/dist/result.mjs:137:44)
Expected behavior
Aborting a streaming run should stop the stream cleanly without throwing, even while it is being iterated. The consumer’s for await loop should end gracefully and no unhandled exception should be raised.
bdjafer