BAML Code Generation
Generate type-safe LLM extraction code. Use when creating structured outputs, classification, RAG, or agent workflows.
Golden Rules
- - NEVER edit
baml_client/ - 100% generated, overwritten on every baml-cli generate; check baml_src/generators.baml for output_type (python, typescript, ruby, go) - ALWAYS edit
baml_src/ - Source of truth for all BAML code - Run
baml-cli generate after changes - Regenerates typed client code for target language
Philosophy (TL;DR)
- - Schema Is The Prompt - Define data models first, compiler injects types
- Types Over Strings - Use enums/classes/unions, not string parsing
- Fuzzy Parsing Is BAML's Job - BAML extracts valid JSON from messy LLM output
- Transpiler Not Library - Write
.baml → generate native code (Python/TypeScript/Ruby/Go), no runtime dependency - Test-Driven Prompting - Use VS Code playground or
baml-cli test to iterate
Workflow
CODEBLOCK0
BAML Syntax
| Element | Example |
|---|
| Class | INLINECODE8 |
| Enum |
enum Category { Tech @alias("technology") @description("Tech sector"), Finance, Other } |
| Function |
function Extract(text: string, img: image?) -> Invoice { client GPT5 prompt #"{{ text }} {{ img }} {{ ctx.output_format }}"# } |
| Client |
client<llm> GPT5 { provider openai options { model gpt-5 } retry_policy Exponential } |
| Fallback |
client<llm> Resilient { provider fallback options { strategy [FastModel, SlowModel] } } |
Types
- - Primitives:
string, int, float, bool | Multimodal: image, INLINECODE18 - Containers:
Type[] (array), Type? (optional), map<string, Type> (key-value) - Composite:
Type1 | Type2 (union), nested classes - Annotations:
@description("..."), @assert(condition), @alias("json_name"), INLINECODE26
Providers
INLINECODE27 , anthropic, gemini, vertex, bedrock, ollama + any OpenAI-compatible via INLINECODE33
Pattern Categories
| Pattern | Use Case | Model | Framework Markers |
|---|
| Extraction | Unstructured → structured | GPT-5 | fastapi, next.js |
| Classification |
Categorization | GPT-5-mini | any |
| RAG | Answers with citations | GPT-5 | langgraph |
| Agents | Multi-step reasoning | GPT-5 | langgraph |
| Vision | Image/audio data extraction | GPT-5-Vision | multimodal |
Resilience
- - retry_policy: INLINECODE34
- fallback client: Chain models
[FastCheap, SlowReliable] for cost/reliability tradeoff
MCP Indicators
- - Found patterns from baml-examples | Validated against BoundaryML/baml | Fixed errors using docs | MCP unavailable, using fallback
Output Artifacts
- 1. BAML Code - Complete
.baml files (types, functions, clients, retry_policy) - Tests - pytest/Jest with 100% function coverage
- Integration - Framework-specific client code (LangGraph nodes, FastAPI endpoints, Next.js API routes)
- Metadata - Pattern used, token count, cost estimate
References
- - providers.md - OpenAI, Anthropic, Google, Ollama, Azure, Bedrock, openai-generic
- types-and-schemas.md - Full type system, classes, enums, unions, map, image, audio
- validation.md - @assert, @check, @alias, block-level @@assert
- patterns.md - Pattern library with code examples
- philosophy.md - BAML principles, golden rules
- mcp-interface.md - Query workflow, caching
- languages-python.md - Python/Pydantic, async
- languages-typescript.md - TypeScript, React/Next.js
- frameworks-langgraph.md - LangGraph integration
BAML 代码生成
生成类型安全的LLM提取代码。在创建结构化输出、分类、RAG或智能体工作流时使用。
黄金法则
- - 切勿编辑 bamlclient/ - 100%自动生成,每次执行 baml-cli generate 时覆盖;检查 bamlsrc/generators.baml 中的 outputtype(python、typescript、ruby、go)
- 始终编辑 bamlsrc/ - 所有BAML代码的真实来源
- 修改后运行 baml-cli generate - 为目标语言重新生成类型化客户端代码
核心理念(TL;DR)
- - 模式即提示词 - 先定义数据模型,编译器注入类型
- 类型优于字符串 - 使用枚举/类/联合类型,而非字符串解析
- 模糊解析是BAML的职责 - BAML从混乱的LLM输出中提取有效JSON
- 转译器而非库 - 编写 .baml → 生成本地代码(Python/TypeScript/Ruby/Go),无运行时依赖
- 测试驱动提示 - 使用VS Code游乐场或 baml-cli test 进行迭代
工作流
分析 → 模式匹配(MCP)→ 验证 → 生成 → 测试 → 交付
↓ [如有错误] 错误恢复(MCP)→ 重试
BAML语法
| 元素 | 示例 |
|---|
| 类 | class Invoice { total float @description(金额) @assert(this > 0) @alias(amt) } |
| 枚举 |
enum Category { Tech @alias(technology) @description(科技行业), Finance, Other } |
| 函数 | function Extract(text: string, img: image?) -> Invoice { client GPT5 prompt #{{ text }} {{ img }} {{ ctx.output_format }}# } |
| 客户端 | client
GPT5 { provider openai options { model gpt-5 } retry_policy Exponential } |
| 回退 | client Resilient { provider fallback options { strategy [FastModel, SlowModel] } } |
类型
- - 基本类型:string、int、float、bool | 多模态:image、audio
- 容器:Type[](数组)、Type?(可选)、map(键值对)
- 复合类型:Type1 | Type2(联合类型)、嵌套类
- 注解:@description(...)、@assert(condition)、@alias(json_name)、@check(name, condition)
提供商
openai、anthropic、gemini、vertex、bedrock、ollama + 通过 openai-generic 兼容任何OpenAI兼容提供商
模式类别
| 模式 | 用例 | 模型 | 框架标记 |
|---|
| 提取 | 非结构化 → 结构化 | GPT-5 | fastapi、next.js |
| 分类 |
分类任务 | GPT-5-mini | any |
| RAG | 带引用的回答 | GPT-5 | langgraph |
| 智能体 | 多步推理 | GPT-5 | langgraph |
| 视觉 | 图像/音频数据提取 | GPT-5-Vision | multimodal |
弹性机制
- - 重试策略:retrypolicy Exp { maxretries 3 strategy { type exponential_backoff } }
- 回退客户端:链式模型 [FastCheap, SlowReliable] 实现成本/可靠性权衡
MCP指示器
- - 从baml-examples找到模式 | 根据BoundaryML/baml验证 | 使用文档修复错误 | MCP不可用,使用回退方案
输出产物
- 1. BAML代码 - 完整的 .baml 文件(类型、函数、客户端、重试策略)
- 测试 - 100%函数覆盖率的pytest/Jest测试
- 集成 - 框架特定的客户端代码(LangGraph节点、FastAPI端点、Next.js API路由)
- 元数据 - 使用的模式、token数量、成本估算
参考文档