Basic Streaming
Useb.stream.FunctionName() to stream responses:
- Python
- TypeScript
- Go
- Ruby
Partial Types
BAML generates partial types for streaming in thepartial_types module. By default:
- All class fields become nullable in partial types
- Fields are filled with non-null values as tokens arrive
- The final result is validated against your original type
- Python
- TypeScript
Stream Request
Use.stream_request to get the HTTP request for streaming without actually sending it:
- Python
- TypeScript
Parse Stream
Parse streaming responses yourself using.parse_stream:
- Python
- TypeScript
Streaming with Options
Pass options to streaming calls just like regular calls:- Python
- TypeScript
- Go
Stream Behavior
Partial Updates
As the LLM streams tokens, BAML:- Accumulates the raw JSON text
- Attempts to parse partial JSON into your defined types
- Fills fields with values as they become available
- Emits partial results that can be displayed immediately
Example Stream Progression
For aResume type, you might see:
Final Response
The final response fromget_final_response() / getFinalResponse():
- Is fully validated against your original BAML types
- Throws validation errors if the LLM output doesn’t match your schema
- Returns the non-nullable, complete type
Error Handling
Streaming can throw errors:- Python
- TypeScript
- Go
Best Practices
- Use streaming for long responses - Better UX when generating large amounts of structured data
- Handle partial data gracefully - Check for
null/Nonefields in partial results - Display progress incrementally - Update UI as partial results arrive
- Always call
get_final_response()- Ensures full validation of the complete result - Handle errors - Stream can fail at any point during generation
Related
- Calling Functions - Basic function call API
- Client Options - Configure streaming behavior
- Error Types - Handle streaming errors