Skip to main content

Hook Input

The HookInput type defines configuration options for BAML React hooks.

Usage​

function Component() {
const hook = useTestAws({
stream: true,
onStreamData: (text) => console.log("Streaming:", text),
onFinalData: (text) => console.log("Complete:", text),
onData: (text) => console.log("Any update:", text),
onError: (error) => console.error("Error:", error)
});

return <div>{hook.data}</div>;
}

Type Definition​

type HookInput<FunctionName, Options extends { stream?: boolean } = { stream?: true }> = {
stream?: Options['stream']
onStreamData?: Options['stream'] extends false
? never
: (response?: StreamDataType<FunctionName>) => void
onFinalData?: (response?: FinalDataType<FunctionName>) => void
onData?: (response?: StreamDataType<FunctionName> | FinalDataType<FunctionName>) => void
onError?: (error: BamlErrors) => void
}

Type Parameters​

ParameterTypeDescription
FunctionNamegenericBAML function name, used to infer response types
Options{ stream?: boolean }Streaming behavior config. Defaults to { stream?: true }

Properties​

PropertyTypeDescription
streamboolean | undefinedEnable streaming mode. Default: true
onStreamData(response?) => voidCallback for streaming responses. Only available when stream is true
onFinalData(response?) => voidCallback for final response
onData(response?) => voidUnified callback for both streaming and final responses
onError(error: BamlErrors) => voidCallback for error handling

Examples​

Streaming Configuration​

const streamingInput: HookInput<'TestAws', { stream: true }> = {
stream: true,
onStreamData: (text) => console.log("Streaming:", text),
onFinalData: (text) => console.log("Final:", text),
onData: (text) => console.log("Any update:", text),
onError: (error) => console.error(error),
};

Non-Streaming Configuration​

const nonStreamingInput: HookInput<'TestAws', { stream: false }> = {
stream: false,
onFinalData: (text) => console.log("Result:", text),
onData: (text) => console.log("Result:", text),
onError: (error) => console.error(error)
// onStreamData is not available when stream is false
};

See Also​