Skip to content
Back to skills

Baml Codegen

ASecurity

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.

  • 21 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 7, 2026
ai-agentstypescriptpythongorubyreactnextjsnodefastapiazureapi

Works with

  • cli
  • api
  • mcp

Security analysis

A100/100

Pro scans all 20 files and shows the line behind each finding

Scanned September 7, 2026

npx -y skills add modbender/skill-library-mcp --skill baml-codegen --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Baml Codegen?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Baml Codegen
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/modbender-baml-codegen/badge)](https://www.skillsdirectory.com/skills/modbender-baml-codegen)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
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

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

## BAML Syntax

| Element | Example |
|---------|---------|
| Class | `class Invoice { total float @description("Amount") @assert(this > 0) @alias("amt") }` |
| 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`, `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

| 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**: `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

- [providers.md](references/providers.md) - OpenAI, Anthropic, Google, Ollama, Azure, Bedrock, openai-generic
- [types-and-schemas.md](references/types-and-schemas.md) - Full type system, classes, enums, unions, map, image, audio
- [validation.md](references/validation.md) - @assert, @check, @alias, block-level @@assert
- [patterns.md](references/patterns.md) - Pattern library with code examples
- [philosophy.md](references/philosophy.md) - BAML principles, golden rules
- [mcp-interface.md](references/mcp-interface.md) - Query workflow, caching
- [languages-python.md](references/languages-python.md) - Python/Pydantic, async
- [languages-typescript.md](references/languages-typescript.md) - TypeScript, React/Next.js
- [frameworks-langgraph.md](references/frameworks-langgraph.md) - LangGraph integration

Files in this skill

  • SKILL.md4.8 KB
  • assets/agents/README.md415 B
  • assets/classification/README.md463 B
  • assets/extraction/README.md457 B
  • assets/integrations/README.md480 B
  • assets/rag/README.md458 B
  • references/BAML-REFERENCE-SOURCE.md28.6 KB
  • references/advanced-patterns.md19.4 KB
  • references/examples.md18.6 KB
  • references/frameworks-langgraph.md19.6 KB
  • references/languages-other.md8.3 KB
  • references/languages-python.md13.8 KB
  • references/languages-typescript.md15.3 KB
  • references/mcp-interface.md10.2 KB
  • references/patterns.md3.6 KB
  • references/philosophy.md2.3 KB
  • references/providers.md3 KB
  • references/types-and-schemas.md3.3 KB
  • references/usecase-image-forms.md4.7 KB
  • references/usecase-workflows.md6 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in ai-agents →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.