-
Notifications
You must be signed in to change notification settings - Fork 498
Description
Describe the bug
Cloudflare Workers has a constraint where you can't start async i/o outside of the context of a request (so you can't do a setTimeout/fetch etc until a request comes in). We do this for performance and security reasons.
OpenAI agents starts collecting traces over here
| addTraceProcessor(defaultProcessor()); |
Uncaught Error: Disallowed operation called within global scope. Asynchronous I/O (ex: fetch() or
connect()), setting a timeout, and generating random values are not allowed within global scope.
To fix this error, perform this operation within a handler.
https://developers.cloudflare.com/workers/runtime-apis/handlers/
at null.<anonymous> (server.js:21280:29) in <instance_members_initializer>
at null.<anonymous> (server.js:21281:14) in BatchTraceProcessor
at null.<anonymous> (server.js:21428:25) in defaultProcessor
at null.<anonymous> (server.js:28636:19)
[code: 10021]
(Annoyingly, while this error does show in wrangler dev and in production, it doesn't show with the vite plugin, which is why I missed this until it was reported to me)
So a fix could be that we don't start the batch processor if navigator.userAgent === 'Cloudflare-Workers, and it's up to the developer to add trace collection manually.
Filing this issue to discuss before I start thinking of solutions / sending PRs.
Debug information
- Agents SDK version:
v0.0.2 - Runtime environment
workerd@1.20250604.0
Repro steps
https://github.com/RogutKuba/test-openai-sdk pnpm install && pnpm start
Expected behavior
It, um, shouldn't crash.