AbortSignal / Timeouts
Overview
BAML provides cancellation support for in-flight function calls across all language clients. In TypeScript, this uses the modern AbortSignal API, while other languages use their native patterns.
Language Support
API Reference
TypeScript
Python
Go
Ruby
TypeScript
AbortController Properties
signal: AbortSignal
- Read-only signal that indicates if the controller has been aborted
AbortController Methods
abort(reason?: any): void
- Cancels the associated operation(s) with an optional reason
AbortSignal Static Methods
AbortSignal.timeout(delay: number): AbortSignal
- Creates a signal that automatically aborts after the specified delay in milliseconds
Integration with Streaming
Abort controllers work seamlessly with streaming responses:
TypeScript
Python
Go
Error Types
When an operation is aborted, language-specific errors are thrown:
- TypeScript:
BamlAbortError
- Python:
BamlAbortError
- Go:
context.Canceled
orcontext.DeadlineExceeded
- Ruby: Not supported
See BamlAbortError for detailed error handling information.
Thread Safety
Abort controllers are thread-safe and can be safely shared across multiple operations or threads.
TypeScript
Python
Go
Ruby
The Node.js AbortController
is thread-safe by design.
Examples
Basic Timeout Implementation
TypeScript
Python
Cancelling Multiple Operations
TypeScript
Python
Go
Related Documentation
- User Guide: Abort Controllers - Learn how to use abort controllers
- Error Handling - Handle cancellation errors
- Streaming - Cancel streaming operations
- withOptions - Set default abort controllers