TypeScript bridge

Type mappings, generated client calls, streaming behavior, and TypeScript-specific boundaries.

Compatibility

Generate the TypeScript client with the same BAML toolchain selected by the project. Application code imports generated symbols locally; the runtime package provides the supporting media and execution types.

ConcernTypeScript behavior
CallsGenerated methods are asynchronous and return promises.
NamesBAML function and type names remain visible in generated code.
RegenerationGenerated files are replaced; application edits belong elsewhere.

Type mappings

BAMLTypeScriptNotes
stringstringText values map directly.
int / floatnumberKeep integer constraints in mind at runtime boundaries.
T[]T[]Element types remain explicit.
T?Nullable generated typeDo not use truthiness when zero or empty text is valid.
classGenerated object typeFields preserve their declared names and types.
enumGenerated enumImport the generated symbol instead of duplicating literals.

Generated client

Import b from the generated client directory and await the method matching the BAML function name.

import { b } from "./baml_client"
import type { Category } from "./baml_client/types"

export async function classify(input: string): Promise<Category> {
  return b.ClassifyMessage(input)
}

Streaming

Streaming calls live under b.stream. The generated stream API exposes incremental values and a final typed result; use the adapter appropriate to the application framework at its outer boundary.

const stream = b.stream.ClassifyMessage(input)
const response = stream.toStreamable()

Transition rules

  • Generated client calls return promises; await them before using the typed result.
  • Regenerate after changing a BAML function, class, enum, or generator configuration.
  • Treat partial streaming values separately from the final typed result.

Gotchas

  • Never edit generated client files by hand.
  • Keep secret-bearing client construction on the server side of web applications.
  • Do not use truthiness to test nullable numbers or strings when zero or empty text is valid.
  • Import generated enum symbols instead of duplicating their literal values.

Continue with the function reference to review the source declaration behind generated calls.