AgentSkillsCN

baml-codegen

在生成类型安全的 LLM 提取、分类、RAG 或代理工作流的 BAML 代码时使用此功能——只需根据自然语言需求,即可自动生成包含类型、函数、客户端、测试及框架集成的完整 .baml 文件。借助 MCP 查询官方 BoundaryML 仓库,实时获取各类模式。支持多模态输入(图像、音频)、Python/TypeScript/Ruby/Go 等多种编程语言,覆盖 10 多种框架,实现 50–70% 的令牌优化,编译成功率高达 95% 以上。

SKILL.md
--- frontmatter
name: baml-codegen
description: "Use when generating BAML code for type-safe LLM extraction, classification, RAG, or agent workflows - creates complete .baml files with types, functions, clients, tests, and framework integrations from natural language requirements. Queries official BoundaryML repositories via MCP for real-time patterns. Supports multimodal inputs (images, audio), Python/TypeScript/Ruby/Go, 10+ frameworks, 50-70% token optimization, 95%+ compilation success."
license: "Apache-2.0"
compatibility: "Requires MCP servers: baml_Docs (required), baml_Examples (optional). Works offline with 80% functionality using cached patterns."

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

code
Analyze → Pattern Match (MCP) → Validate → Generate → Test → Deliver
         ↓ [IF ERRORS] Error Recovery (MCP) → Retry

BAML Syntax

ElementExample
Classclass Invoice { total float @description("Amount") @assert(this > 0) @alias("amt") }
Enumenum Category { Tech @alias("technology") @description("Tech sector"), Finance, Other }
Functionfunction Extract(text: string, img: image?) -> Invoice { client GPT5 prompt #"{{ text }} {{ img }} {{ ctx.output_format }}"# }
Clientclient<llm> GPT5 { provider openai options { model gpt-5 } retry_policy Exponential }
Fallbackclient<llm> Resilient { provider fallback options { strategy [FastModel, SlowModel] } }

Types

  • Primitives: string, int, float, bool | Multimodal: image, audio
  • Containers: Type[] (array), Type? (optional), map<string, Type> (key-value)
  • Composite: Type1 | Type2 (union), nested classes
  • Annotations: @description("..."), @assert(condition), @alias("json_name"), @check(name, condition)

Providers

openai, anthropic, gemini, vertex, bedrock, ollama + any OpenAI-compatible via openai-generic

Pattern Categories

PatternUse CaseModelFramework Markers
ExtractionUnstructured → structuredGPT-5fastapi, next.js
ClassificationCategorizationGPT-5-miniany
RAGAnswers with citationsGPT-5langgraph
AgentsMulti-step reasoningGPT-5langgraph
VisionImage/audio data extractionGPT-5-Visionmultimodal

Resilience

  • retry_policy: retry_policy Exp { max_retries 3 strategy { type exponential_backoff } }
  • 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)
  2. Tests - pytest/Jest with 100% function coverage
  3. Integration - Framework-specific client code (LangGraph nodes, FastAPI endpoints, Next.js API routes)
  4. Metadata - Pattern used, token count, cost estimate

References