BAML Integration

BAML (BoundaryML) is a domain-specific language (DSL) designed specifically for building applications with Large Language Models. Unlike traditional approaches that rely on string-based prompts and unreliable JSON parsing, BAML provides a structured, type-safe way to define LLM functions and ensure predictable outputs.
Why BAML? Let’s be honest - Instructor + LiteLLM feels dated. BAML addresses reliability challenges by providing a robust framework for structured LLM interactions, eliminating the need to manage prompts manually and ensuring everything stays in sync.

Key Benefits

Type Safety

Reliable OutputsStructured outputs eliminate parsing errors with declarative BAML syntax and runtime type validation.

Easy Prompt Management

No More BoilerplateAvoid boilerplate needed to manage prompts, read them, version them, and keep everything in sync.

Functional Paradigm

Clean ArchitectureEasily switch between different prompts in a functional paradigm with modularized templates.

Enterprise Ready

Production FeaturesAutomatic retries, error handling, streaming support, and dynamic model switching.

BAML vs Traditional Approaches

Problems with Traditional Approaches

BAML Features in Cognee

1. Knowledge Graph Extraction

Our BAML implementation includes templates for extracting structured knowledge graphs:
function ExtractContentGraphGeneric(
    content: string,
    mode: "simple" | "base" | "guided" | "strict" | "custom"?
) -> KnowledgeGraph {
    client OpenAI
    // Multiple extraction modes for different use cases
}
Multiple Extraction Modes:
Fast & High-Quality
# Simple mode for general use cases
graph = await LLMGateway.extract_content_graph(
    content="Apple Inc. was founded by Steve Jobs in Cupertino.",
    response_model=KnowledgeGraph,
    mode="simple"
)

# Fast extraction with good quality
# Best for: General content, quick processing

2. Content Classification

Content type detection with structured outputs:
class ContentLabel {
    content_type "text" | "audio" | "image" | "video" | "multimedia" | "3d_model" | "procedural"
    type string
    subclass string[]
}

function ClassifyContent(content: string) -> ContentLabel {
    client OpenAI
    // Automatic content type detection
}

3. Code Summarization

Specialized code understanding with structured analysis:
class SummarizedCode {
    high_level_summary string
    key_features string[]
    imports string[]
    classes SummarizedClass[]
    functions SummarizedFunction[]
    workflow_description string?
}

function SummarizeCode(code: string) -> SummarizedCode {
    client OpenAI
    // Structured code analysis
}

Configuration

Environment Setup

Configure BAML as your structured output framework:
# Set BAML as the structured output framework
export STRUCTURED_OUTPUT_FRAMEWORK=BAML

# Configure your LLM provider (use existing Cognee LLM config)
export LLM_PROVIDER=openai
export LLM_API_KEY=your_openai_api_key
export LLM_MODEL=gpt-4o-mini

Framework Selection Logic

Cognee automatically selects the appropriate framework based on your configuration:
Smart Framework Switching
def extract_content_graph(content: str, response_model: Type[BaseModel], mode: str = "simple"):
    llm_config = get_llm_config()
    
    if llm_config.structured_output_framework.upper() == "BAML":
        # Use BAML implementation with advanced features
        return extract_content_graph_baml(
            content=content, 
            response_model=response_model, 
            mode=mode
        )
    else:
        # Fall back to Instructor implementation
        return extract_content_graph_instructor(
            content=content, 
            response_model=response_model
        )

Developer Experience

Type Safety Benefits

Getting Started with BAML

1

Configure Framework

Set BAML as your structured output framework in environment variables.
2

Set LLM Provider

Configure your preferred LLM provider (OpenAI, Anthropic, etc.) for BAML.
3

Choose Extraction Mode

Select the appropriate extraction mode for your use case (simple, guided, strict, custom).
4

Extract Knowledge

Use BAML-powered extraction in your Cognee pipeline for reliable results.

Configuration Example

# Set BAML as the structured output framework
export STRUCTURED_OUTPUT_FRAMEWORK=BAML

# Use existing Cognee LLM configuration
export LLM_PROVIDER=openai
export LLM_API_KEY=your_openai_api_key
export LLM_MODEL=gpt-4o-mini

Advanced BAML Features

Flexible Processing
from cognee.infrastructure.llm import LLMGateway

# Compare different extraction modes
content = "Tesla revolutionized electric vehicles with the Model S, Model 3, and Cybertruck."

# Simple mode - fast and reliable
simple_result = await LLMGateway.extract_content_graph(
    content=content,
    response_model=KnowledgeGraph,
    mode="simple"
)

# Strict mode - conservative extraction
strict_result = await LLMGateway.extract_content_graph(
    content=content,
    response_model=KnowledgeGraph,
    mode="strict"
)

# Guided mode - detailed instructions
guided_result = await LLMGateway.extract_content_graph(
    content=content,
    response_model=KnowledgeGraph,
    mode="guided"
)

print(f"Simple mode: {len(simple_result.entities)} entities")
print(f"Strict mode: {len(strict_result.entities)} entities")
print(f"Guided mode: {len(guided_result.entities)} entities")

Integration with Cognee Pipeline

Automatic Integration

BAML Advantages in Practice

Reliability

Production Ready
# No more JSON parsing errors
try:
    graph = await baml.ExtractContentGraphGeneric(
        content=content, mode="strict"
    )
    # Always get properly typed results
except Exception as e:
    # Structured error handling
    print(f"Extraction failed: {e}")

Maintainability

Easy Updates
// Update prompts in BAML files
function ExtractEntities(content: string) -> EntityList {
    client OpenAI
    prompt #"
        Updated extraction logic here...
    "#
}
// No code changes needed!

Testing

Built-in Testing
# Test BAML functions
test_result = await baml.test_function(
    function_name="ExtractContentGraphGeneric",
    test_cases=[
        {"content": "test input", "expected_entities": 3}
    ]
)

Performance

Optimized Execution
# Streaming support for large content
async for chunk in baml.stream_extraction(
    content=large_document,
    mode="simple"
):
    await process_chunk(chunk)

Troubleshooting

Configuration Issues

Looking Forward

Future BAML Features: We’re working on exciting enhancements including:
  • On-the-fly recompilation: Update BAML functions without restarts
  • Search customization: BAML-powered search result enhancement
  • Dynamic pipelines: Runtime pipeline modification with BAML
  • Advanced modeling: More sophisticated knowledge graph models

Why We Love BAML

IDE Integration & Type SafetyThe BAML IDE client is a pleasure to work with:
  • Full autocomplete and IntelliSense
  • Real-time error checking
  • Integrated testing and debugging
  • Visual prompt editing

Next Steps

Ready to try BAML in Cognee? Set STRUCTURED_OUTPUT_FRAMEWORK=BAML in your environment and start building more reliable AI applications today!