Generated Hooks Reference

BAML automatically generates a type-safe React hook for each BAML function. Each hook follows the naming pattern use{FunctionName} and supports both streaming and non-streaming modes.

1import { useWriteMeAStory } from "@/baml_client/react/hooks";
2
3// Basic usage with streaming enabled by default
4const hook = useWriteMeAStory();
5
6// Access streaming and final data
7const { data, streamData, finalData } = hook;
8
9// Track request state
10const { isLoading, isStreaming, isPending, isSuccess, isError } = hook;
11
12// Execute the function
13await hook.mutate("A story about a brave AI");
14
15// Reset state if needed
16hook.reset();

HookInput

The hook accepts an optional configuration object. See Hook Input for complete details.

stream
boolean

Enable streaming mode for real-time updates. Defaults to true.

onStreamData
(response?: StreamDataType<FunctionName>) => void

Callback for streaming updates. Only available when streaming is enabled.

onFinalData
(response?: FinalDataType<FunctionName>) => void

Callback when the request completes.

onData
(response?: StreamDataType<FunctionName> | FinalDataType<FunctionName>) => void

Unified callback for both streaming and final responses.

onError
(error: BamlErrors) => void

Callback when an error occurs. See Error Types.

HookOutput

The hook returns an object with the following properties. See Hook Output for complete details.

data
FinalDataType<FunctionName> | StreamDataType<FunctionName> | undefined

The current response data. Contains either streaming or final data depending on the request state.

finalData
FinalDataType<FunctionName> | undefined

The final response data. Only available when the request completes.

streamData
StreamDataType<FunctionName> | undefined

Latest streaming update. Only available in streaming mode.

error
BamlErrors | undefined

Error information if the request fails. See Error Types.

isLoading
boolean

True while the request is in progress (either pending or streaming).

isPending
boolean

True if the request is pending (not yet streaming or completed).

isStreaming
boolean

True if the request is currently streaming data. Only available in streaming mode.

isSuccess
boolean

True if the request completed successfully.

isError
boolean

True if the request failed.

status
HookStatus<Options>

Current state of the request. For streaming hooks: ‘idle’ | ‘pending’ | ‘streaming’ | ‘success’ | ‘error’. For non-streaming hooks: ‘idle’ | ‘pending’ | ‘success’ | ‘error’.

mutate
(...args: Parameters<ServerAction>) => Promise<OutputType>

Function to execute the BAML function. Returns a ReadableStream for streaming hooks, or a Promise of the final response for non-streaming hooks.

reset
() => void

Function to reset the hook state back to its initial values.

Built with