feat: implement AbortController in py, ts, go, wasm (also cancel buttons)#2357
feat: implement AbortController in py, ts, go, wasm (also cancel buttons)#2357
Conversation
- Added Cancelled variant to LLMResponse enum - Integrated stream-cancel Tripwire for cancellation checks - Updated orchestrators (sync and streaming) to accept optional Tripwire - Handled Cancelled variant in all match statements across 8 files - Prepared foundation for language-specific abort handler bridges Currently passing None for cancellation tokens - ready for language bridges in Phase 2-4. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
Implemented context cancellation propagation from Go to Rust: CFFI Layer: - Added cancel_function_call extern function for Go to call - Integrated stream-cancel crate with Tripwire for cancellation - Used tokio::select! to handle cancellation at CFFI layer - Track active operations with DashMap for cancellation Go Client: - Added CancelFunctionCall export function - Updated callbacks to call cancel on context.Done() - Fixed streaming template to pass user context directly - Added wrapper functions for dynamic library loading Testing: - Created comprehensive manual test suite - All cancellation tests passing: ✓ Context cancellation (~100ms) ✓ Streaming cancellation (~50ms) ✓ Timeout cancellation (~200ms) ✓ Minimal goroutine leaks The implementation allows Go applications to cancel in-flight BAML operations using standard context.Context patterns, properly cleaning up resources. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
…tion Implemented early cancellation detection pattern for Go language support: - Modified runtime.go to monitor context.Done() immediately on function call - Removed redundant late cancellation from callbacks.go - Updated test suite to use correct BAML client functions - All tests passing with responsive cancellation (~100ms response time) Key improvement: Cancellation now happens immediately when Go context is cancelled, not waiting for data callbacks from Rust. This provides much more responsive abort handling. Updated implementation plan with Phase 2 completion and clear instructions for Phase 3 (TypeScript) implementation. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
- Implement AbortController for Python and TypeScript clients - Add cancellation support via tripwires in Rust runtime - Update all generated client code with abort handler APIs - Add comprehensive tests for abort functionality - Clean up disabled test files
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
@sxlijin @hellovai I see one problem with the API added to the TypeScript SDK in this PR. The operations expect an After making that change, the TypeScript snippet from the PR description could be simplified as follows: const promise = b.ExtractName("John Doe", {
signal: AbortSignal.timeout(100)
});
try {
await promise;
} catch (e) {
// For consistency, I think we should also use the built-in `AbortError`
// class here, instead of a custom one.
if (e instanceof AbortError) {
console.log("Operation cancelled");
}
}In my opinion, it would be a good idea to make these changes before a new release; otherwise, it could be a breaking change. |
|
@trojanowski thanks for this! we actually patched it to be just that! #2373 shows that off! this was a really really great catch :) |
|
@hellovai that's really great, but for consistency with other APIs, I would suggest two additional changes:
|
|
thanks @trojanowski
Oh! interesting, we explicitly left it as
We sadly can't do this as there is no such thing as |
Implement AbortController in python, typescript, go, and also wasm. Also implement cancel functionality for run test in wasm and add docs for py/ts/go.
First 80% of this work done by @dexhorthy and @hellovai in #2331 :
Built in close collaboration with @hellovai who sat in my apartment and shouted at claude with me all day
Note: plan/Research artifacts in thoughts/ can be removed once they get put in a good location, leaving them in for now
What problem(s) was I solving?
Previously, once a BAML function call was initiated, there was no way to cancel it. This meant:
What user-facing changes did I ship?
Added abort/cancellation support for all BAML operations using native language patterns:
TypeScript
Python
Go
How I implemented it
stream-cancelcrate'sTripwiremechanismAbortControllersupport via NAPI bridgeAbortControllerclass exposed via PyO3context.Contextcancellation with early detection goroutinesLLMResponse::Cancelledvariant to handle cancelled operationsHow to verify it
Run the tests:
Test status:
make check testpassesAlso re-enabled 6 previously disabled test files (PDF, video, OpenAI responses, expression functions) that are now compatible.
Description for the changelog
Added comprehensive abort handler support for TypeScript, Python, and Go, allowing cancellation of in-flight LLM operations through native language cancellation patterns.