BAML x cognee: Type-Safe LLMs in Production
We’ve added BAML (from BoundaryML) as a first-class structured-output framework in cognee! BAML gives you a type-safe, schema-driven way to define LLM functions, validate responses, and version prompts—without fragile JSON parsing or excess boilerplate.
BAML lives alongside our existing Instructor support, and you can switch between these two frameworks with a single config flag, with no pipeline rewrites.
Teams using cognee to extract knowledge graphs, classify content, or summarize code now get the more reliable, validated outputs and easier prompt lifecycle management that this integration brings.
What Is BAML?
BAML is a domain-specific language (DSL) for building applications with LLMs. It ships a typed client and a DSL that enforce contracts at runtime. Instead of relying on free-form prompts and post-hoc JSON parsing, BAML lets you:
- Declare schemas and function contracts for LLM calls.
- Enforce types at runtime (and benefit from IDE autocompletion).
- Package and version prompts like code so teams can review changes via PRs.
- Swap providers/models via a client registry without touching call sites.
As a result, you get predictable outputs, simpler error handling, and a smoother path to production for LLM features.
Why Bring BAML Into cognee?
Many of our users have been asking for stronger guarantees around structured outputs and less prompt drift as projects grow.
While Instructor + LiteLLM remain supported and work well for many workloads, they are starting to feel outdated. BAML adds a complementary, opinionated model that reduces the boilerplate needed to manage prompts and centralizes schema + prompt management.
With BAML in cognee, you get:
- Reliability: BAML uses a json-repair algorithm to parse and validate outputs.
- Maintainability: Prompts and extraction logic live in a declarative DSL, easy to diff and review.
- Speed: Fewer glue layers (retries, validation, streaming) to hand-roll.
- Flexibility: Choose BAML or Instructor per environment via config, and keep shipping.
This integration is all about giving you the choice of output style that best suits your team and workload, while not burdening you with having to change how you design pipelines in cognee.
Use BAML when you need stricter guarantees (compliance, audits, multi-team); use Instructor for rapid prototyping where you prefer looser constraints.
What Ships in This Release
cognee's BAML integration is elegantly simple: it dynamically transforms your Pydantic models into BAML types and provides a single unified function for structured LLM outputs.
Here's how it works under the hood:
- Dynamic Type Mapping: Your Pydantic models are automatically converted to BAML class definitions at runtime
- Single Function Interface: One BAML function (AcreateStructuredOutput) handles all structured output requests
- Automatic Validation: Responses are validated against your Pydantic schema before being returned
The BAML Function
The ResponseModel is dynamically generated from your Pydantic model, ensuring type safety without manual schema duplication.
Dynamic Type Generation
The magic happens in the type mapping system. When you pass a Pydantic model to cognee, it:
- Maps primitive types: str, int, float, bool, datetime
- Handles collections: List[T], Dict[str, T]
- Supports optionals: Optional[T] and Union types
- Processes enums: Python Enum classes become BAML enums
- Recursively handles nested models: Complex Pydantic models with nested structures
Implementation Notes (Clean, Swappable)
Framework Selection (No Recursion, Clear Fallback)
We expose a single entry point and route to the chosen framework at runtime:
Why this pattern?
- No accidental recursion (the router never calls itself).
- Testable boundaries (you can unit-test each backend independently).
- Easy A/B (flip a flag per env to compare outputs/latency/cost).
Generated Client Behavior (What You Can Rely On)
BAML’s generated Python clients provide:
- Sync / async variants for pipelines and services.
- Automatic retries & backoff for transient failures.
- Streaming support for large responses.
- Runtime type validation for every response.
Tip: expose retry/backoff knobs and max token settings via config so ops can tune them without code changes.
How This Benefits Developers
- Fewer parsing bugs: You work with typed objects, not ad-hoc JSON.
- Faster iteration: Swap prompts/models via config; treat prompts & schemas like code (review, version, roll back).
- Lower boilerplate: Retries, validation, streaming are built-in.
- Safer refactors: IDE-friendly types surface breaking changes in tests—not in prod.
- Zero schema duplication: Your Pydantic models automatically become BAML types.
Quickstart Guide
0) Prereqs
- cognee updated to the version that includes BAML support.
- BAML installed & initialized (see BoundaryML docs).
- Provider API key set (OpenAI/Bedrock/Anthropic/Azure, etc.).
- Network egress to your LLM provider.
1) Pick Your Framework & Model
Cognee uses OpenAI as the default embedding model provider. Refer to our .env.template for other options.
2) Use Clearly Defined Pydantic Models
Note that types need to be defined clearly. Pydantic models that don’t have specific types won’t work as there is no mapping in BAML to handle it.
End-to-End Example: From Text to Validated Knowledge Graph
0) Environment & Config
1) Imports & Model Types
2) Wire Up cognee (BAML routing happens under the hood)
You should see a validated KnowledgeGraph with nodes like Person:Alice, Book:The Graph Handbook, Store:CityBooks, City:Seattle, Person:Bob, and edges such as PURCHASED, REVIEWED, RECOMMENDED_TO, LOCATED_IN (with properties like date).
3) Inspect & Sanity-Check
Migrating From Instructor (Minimal Changes):
- Keep your Pydantic models. They become the response schema for BAML as-is.
- Flip the framework flag (STRUCTURED_OUTPUT_FRAMEWORK=BAML).
- Optional: Move prompt logic into BAML DSL so you can version & review it independently.
Structured Outputs for a Scalable Future
We built this integration to remove daily dev paper-cuts: brittle prompts, silent schema drift, surprise JSON failures. With BAML in cognee, you get production hygiene: fewer parsing errors, clearer versioning, safer changes, faster iteration.
BAML’s DSL lets you treat prompts like code—typed, diffable, testable—while cognee turns those outputs into knowledge graphs, classification, and code summaries you can trust. Keep using Instructor where it fits; switch to BAML where type-safety and governance matter most.
Ready to try it for yourself?
- See our implementation in the cognee repo
- Learn more about BAML in BoundaryML's documentation
Thanks & Credits
Huge thanks to the BAML team for their round-the-clock support and quick feedback during integration. We deeply appreciate how engaged and hard working they are and we’re excited about what this update unlocks for cognee users worldwide.

BAML x cognee: Type-Safe LLMs in Production

LangGraph × cognee: Enhancing Agents with Persistent, Queryable Memory
