OnTick
The onTick feature allows you to receive real-time callbacks during BAML function execution, providing access to internal state, streaming responses, and progress updates. This is particularly useful for monitoring function progress, debugging, and accessing intermediate data like “thinking” content from streaming LLM responses.
Quick Start
Python
TypeScript
Go
from baml_client import bfrom baml_py import baml_pydef on_tick(reason: str, log: baml_py.FunctionLog):print(f"Tick received: {reason}")print(f"Function calls: {len(log.calls) if log else 0}")# Use with async functionresult = await b.TestFunction("Hello world", baml_options={"on_tick": on_tick})
import { b } from 'baml_client'import type { FunctionLog } from '@boundaryml/baml'type TickReason = "Unknown"const onTick = (reason: TickReason, log: FunctionLog | null) => {console.log(`Tick received: ${reason}`)console.log(`Function calls: ${log?.calls?.length || 0}`)}// Use with async functionconst result = await b.TestFunction("Hello world", { onTick })
import ("fmt"b "my_project/baml_client"baml "github.com/boundaryml/baml/engine/language_client_go/pkg")func onTick(reason string, log *baml.FunctionLog) {fmt.Printf("Tick received: %s\n", reason)if log != nil {fmt.Printf("Function calls: %d\n", len(log.Calls))}}// Use with function callresult, err := b.TestFunction(ctx, "Hello world", b.WithOnTick(onTick))
Common Use Cases
Progress Monitoring
Track the progress of long-running BAML function calls:
Python
TypeScript
Go
from baml_client import bfrom baml_py import baml_pydef progress_monitor(reason: str, log: baml_py.FunctionLog):tick_count = getattr(progress_monitor, 'count', 0)progress_monitor.count = tick_count + 1print(f"Progress tick #{progress_monitor.count}: {reason}")if log and log.calls:latest_call = log.calls[-1]print(f"Latest call to: {latest_call.client_name}")result = await b.ExtractResume(resume_text,baml_options={"on_tick": progress_monitor})
import { b } from 'baml_client'import type { FunctionLog } from '@boundaryml/baml'let tickCount = 0const progressMonitor = (reason: string, log: FunctionLog | null) => {tickCount++console.log(`Progress tick #${tickCount}: ${reason}`)if (log?.calls?.length) {const latestCall = log.calls[log.calls.length - 1]console.log(`Latest call to: ${latestCall.clientName}`)}}const result = await b.ExtractResume(resumeText, { onTick: progressMonitor })
var tickCount intfunc progressMonitor(reason string, log *baml.FunctionLog) {tickCount++fmt.Printf("Progress tick #%d: %s\n", tickCount, reason)if log != nil && len(log.Calls) > 0 {latestCall := log.Calls[len(log.Calls)-1]fmt.Printf("Latest call to: %s\n", latestCall.ClientName)}}result, err := b.ExtractResume(ctx, resumeText, baml.WithOnTick(progressMonitor))
Accessing Streaming “Thinking” Content
Extract intermediate “thinking” content from streaming LLM responses:
Python
TypeScript
Go
import jsonfrom baml_client import bfrom baml_py import baml_pydef extract_thinking(reason: str, log: baml_py.FunctionLog):thinking_content = ""if log and log.calls:last_call = log.calls[-1]# Check if it's a streaming callif hasattr(last_call, "sse_responses"):sse_responses = last_call.sse_responses()if sse_responses:for response in sse_responses:try:data = json.loads(response.text)if "delta" in data and "thinking" in data["delta"]:thinking_content += data["delta"]["thinking"]except (json.JSONDecodeError, AttributeError):passif thinking_content:print(f"Thinking content: {thinking_content}")# Use with streaming functionstream = b.stream.TestThinking("Write a story about AI",baml_options={"on_tick": extract_thinking})async for msg in stream:passresult = await stream.get_final_response()
import { b } from 'baml_client'import type { FunctionLog, LlmStreamCall } from '@boundaryml/baml'const extractThinking = (reason: string, log: FunctionLog | null) => {let thinkingContent = ""if (log?.calls?.length) {const lastCall = log.calls[log.calls.length - 1]// Check if it's a stream callif ('sseResponses' in lastCall) {const streamCall = lastCall as LlmStreamCallconst responses = streamCall.sseResponses()if (responses) {for (const response of responses) {try {const data = JSON.parse(response.text)if (data.delta?.thinking) {thinkingContent += data.delta.thinking}} catch {// Ignore parse errors}}}}}if (thinkingContent) {console.log(`Thinking content: ${thinkingContent}`)}}// Use with streaming functionconst stream = b.stream.TestThinking("Write a story about AI", { onTick: extractThinking })for await (const msg of stream) {// Process streaming messages}const result = await stream.getFinalResponse()
import ("encoding/json""fmt""github.com/BoundaryML/baml/baml-go")func extractThinking(reason string, log *baml.FunctionLog) {thinkingContent := ""if log != nil && len(log.Calls) > 0 {lastCall := log.Calls[len(log.Calls)-1]// Check if it's a streaming callif streamCall, ok := lastCall.(*baml.LLMStreamCall); ok {responses := streamCall.SSEResponses()for _, response := range responses {var data map[string]interface{}if err := json.Unmarshal([]byte(response.Text), &data); err == nil {if delta, ok := data["delta"].(map[string]interface{}); ok {if thinking, ok := delta["thinking"].(string); ok {thinkingContent += thinking}}}}}}if thinkingContent != "" {fmt.Printf("Thinking content: %s\n", thinkingContent)}}// Use with streaming functionstream, err := b.StreamTestThinking(ctx, "Write a story about AI", baml.WithOnTick(extractThinking))if err != nil {return err}for msg := range stream.Channel() {// Process streaming messages}result := stream.FinalResponse()
Debugging and Logging
Use onTick for comprehensive debugging and logging:
Python
TypeScript
Go
from baml_client import bfrom baml_py import baml_pydef debug_logger(reason: str, log: baml_py.FunctionLog):print(f"=== DEBUG TICK: {reason} ===")if log:print(f"Function: {log.function_name}")print(f"Log type: {log.log_type}")print(f"Number of calls: {len(log.calls)}")if log.usage:print(f"Input tokens: {log.usage.input_tokens}")print(f"Output tokens: {log.usage.output_tokens}")if log.calls:latest_call = log.calls[-1]print(f"Latest provider: {latest_call.provider}")print(f"Latest client: {latest_call.client_name}")if latest_call.usage:print(f"Call usage - Input: {latest_call.usage.input_tokens}, Output: {latest_call.usage.output_tokens}")print("=== END DEBUG ===\n")result = await b.TestFunction("Debug this call", baml_options={"on_tick": debug_logger})
import { b } from 'baml_client'import type { FunctionLog } from '@boundaryml/baml'const debugLogger = (reason: string, log: FunctionLog | null) => {console.log(`=== DEBUG TICK: ${reason} ===`)if (log) {console.log(`Function: ${log.functionName}`)console.log(`Log type: ${log.logType}`)console.log(`Number of calls: ${log.calls?.length || 0}`)if (log.usage) {console.log(`Input tokens: ${log.usage.inputTokens}`)console.log(`Output tokens: ${log.usage.outputTokens}`)}if (log.calls?.length) {const latestCall = log.calls[log.calls.length - 1]console.log(`Latest provider: ${latestCall.provider}`)console.log(`Latest client: ${latestCall.clientName}`)if (latestCall.usage) {console.log(`Call usage - Input: ${latestCall.usage.inputTokens}, Output: ${latestCall.usage.outputTokens}`)}}}console.log("=== END DEBUG ===\n")}const result = await b.TestFunction("Debug this call", { onTick: debugLogger })
func debugLogger(reason string, log *baml.FunctionLog) {fmt.Printf("=== DEBUG TICK: %s ===\n", reason)if log != nil {fmt.Printf("Function: %s\n", log.FunctionName)fmt.Printf("Log type: %s\n", log.LogType)fmt.Printf("Number of calls: %d\n", len(log.Calls))if log.Usage != nil {fmt.Printf("Input tokens: %d\n", log.Usage.InputTokens)fmt.Printf("Output tokens: %d\n", log.Usage.OutputTokens)}if len(log.Calls) > 0 {latestCall := log.Calls[len(log.Calls)-1]fmt.Printf("Latest provider: %s\n", latestCall.Provider)fmt.Printf("Latest client: %s\n", latestCall.ClientName)if latestCall.Usage != nil {fmt.Printf("Call usage - Input: %d, Output: %d\n",latestCall.Usage.InputTokens,latestCall.Usage.OutputTokens)}}}fmt.Println("=== END DEBUG ===\n")}result, err := b.TestFunction(ctx, "Debug this call", baml.WithOnTick(debugLogger))
Using with Collectors
OnTick can be used alongside Collectors for comprehensive logging:
Python
TypeScript
Go
from baml_client import bfrom baml_py import baml_py, Collectordef on_tick_with_collector(reason: str, log: baml_py.FunctionLog):print(f"OnTick fired: {reason}")# Create a collector alongside onTickcollector = Collector("my-collector")result = await b.TestFunction("Hello world",baml_options={"on_tick": on_tick_with_collector,"collector": collector})# Access data through both mechanismsprint(f"Collector usage: {collector.last.usage}")
import { b } from 'baml_client'import { Collector } from '@boundaryml/baml'import type { FunctionLog } from '@boundaryml/baml'const onTickWithCollector = (reason: string, log: FunctionLog | null) => {console.log(`OnTick fired: ${reason}`)}// Create a collector alongside onTickconst collector = new Collector("my-collector")const result = await b.TestFunction("Hello world", {onTick: onTickWithCollector,collector})// Access data through both mechanismsconsole.log(`Collector usage: ${collector.last?.usage}`)
func onTickWithCollector(reason string, log *baml.FunctionLog) {fmt.Printf("OnTick fired: %s\n", reason)}// Create a collector alongside onTickcollector, err := baml.NewCollector("my-collector")if err != nil {return err}result, err := b.TestFunction(ctx, "Hello world",baml.WithOnTick(onTickWithCollector),baml.WithCollector(collector),)// Access data through both mechanismsfmt.Printf("Collector usage: %v\n", collector.Last().Usage)
Error Handling
OnTick callbacks should handle errors gracefully. If an onTick callback throws an error, the function execution will continue:
Python
TypeScript
Go
from baml_client import bfrom baml_py import baml_pydef error_prone_tick(reason: str, log: baml_py.FunctionLog):# Simulate an error conditionif hasattr(error_prone_tick, 'count'):error_prone_tick.count += 1else:error_prone_tick.count = 1if error_prone_tick.count == 5:raise ValueError("Intentional error in onTick")print(f"Tick #{error_prone_tick.count}: {reason}")# Function will complete despite callback errorsresult = await b.TestFunction("Hello world", baml_options={"on_tick": error_prone_tick})print("Function completed successfully despite onTick error")
import { b } from 'baml_client'import type { FunctionLog } from '@boundaryml/baml'let tickCount = 0const errorProneTick = (reason: string, log: FunctionLog | null) => {tickCount++if (tickCount === 5) {throw new Error("Intentional error in onTick")}console.log(`Tick #${tickCount}: ${reason}`)}// Function will complete despite callback errorsconst result = await b.TestFunction("Hello world", { onTick: errorProneTick })console.log("Function completed successfully despite onTick error")
var tickCount intfunc errorProneTick(reason string, log *baml.FunctionLog) {tickCount++if tickCount == 5 {panic("Intentional error in onTick") // In Go, you might handle this differently}fmt.Printf("Tick #%d: %s\n", tickCount, reason)}// Function will complete despite callback errorsresult, err := b.TestFunction(ctx, "Hello world", baml.WithOnTick(errorProneTick))if err == nil {fmt.Println("Function completed successfully despite onTick error")}
Limitations
Keep these limitations in mind when using onTick:
-
Synchronous Functions: OnTick is not supported for synchronous function calls. Attempting to use onTick with sync functions will throw an error.
-
Error Isolation: Errors in onTick callbacks do not stop function execution, but they may not be explicitly surfaced.
API Reference
OnTick Callback Signature
Python
TypeScript
Go
def on_tick(reason: str, log: baml_py.FunctionLog | None) -> None:"""OnTick callback functionArgs:reason: The reason for the tick (currently always "Unknown")log: The current function log with call information"""pass
type TickCallback = (reason: TickReason, log: FunctionLog | null) => voidtype TickReason = "Unknown" // Currently only one reason typeinterface BamlCallOptions {onTick?: TickCallback// ... other options}
type TickCallback func(reason string, log *FunctionLog)func WithOnTick(onTick TickCallback) CallOptionFuncfunc WithExperimentalOnTick(onTick TickCallback) CallOptionFunc // Deprecated
Integration with Function Calls
OnTick is passed via the baml_options parameter (Python) or options object (TypeScript/Go):
Python
TypeScript
Go
# Async function callresult = await b.FunctionName(input, baml_options={"on_tick": callback})# Streaming function callstream = b.stream.FunctionName(input, baml_options={"on_tick": callback})
// Async function callconst result = await b.FunctionName(input, { onTick: callback })// Streaming function callconst stream = b.stream.FunctionName(input, { onTick: callback })
// Function callresult, err := b.FunctionName(ctx, input, baml.WithOnTick(callback))// Streaming function callstream, err := b.StreamFunctionName(ctx, input, baml.WithOnTick(callback))
Related Topics
- Collector - Learn about comprehensive logging with Collectors
- Using with_options - Configure global options for BAML functions
- Streaming - Understand streaming function calls
Best Practices
- Keep Callbacks Light: OnTick callbacks should be fast and non-blocking
- Handle Errors Gracefully: Always include error handling in your callbacks
- Use with Collectors: Combine onTick with Collectors for comprehensive logging
- Monitor Performance: Test the performance impact for your specific use case
- Async Only: Remember that onTick only works with async function calls, not sync calls