For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
Help on Discord
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
HomeGuideExamplesBAML ReferencePlaygroundAgents.mdChangelog
    • Overview
  • baml-cli
    • init
    • generate
    • test
    • serve
    • dev
    • fmt
  • Language Reference
    • Types
    • function
    • test
    • template_string
    • client<llm>
    • class
    • enum
    • generator
  • Generated baml_client
    • with_options(..)
    • AbortSignal / Cancellation
    • Collector
    • logging / env vars
    • AsyncClient / SyncClient
    • TypeBuilder
    • ClientRegistry
    • client Option
    • OnTick
    • Multimodal
    • Image
    • Audio
    • Pdf
    • Video
  • Attributes
    • What are attributes?
    • @alias / @@alias
    • @description / @@description
    • @skip
    • @assert
    • @check
    • Jinja in Attributes
    • @@dynamic
  • LLM Client Providers
    • Overview
    • AWS Bedrock
    • Anthropic
    • Google AI: Gemini
    • Google: Vertex
    • OpenAI
    • OpenAI Responses API
    • OpenAI from Azure
    • OpenRouter
    • openai-generic
    • Microsoft Foundry (openai-generic)
    • Cerebras (openai-generic)
    • Groq (openai-generic)
    • Hugging Face (openai-generic)
    • Keywords AI (openai-generic)
    • Llama API (openai-generic)
    • Litellm (openai-generic)
    • LM Studio (openai-generic)
    • Ollama (openai-generic)
    • Vercel AI Gateway (openai-generic)
    • Tinfoil (openai-generic)
    • TogetherAI (openai-generic)
    • Unify AI (openai-generic)
    • vLLM (openai-generic)
  • LLM Client Strategies
    • Timeout Configuration
    • Retry Policy
    • Fallback
    • Round Robin
  • Prompt Syntax
    • What is jinja?
    • Jinja Filters
    • ctx.output_format
    • ctx.client
    • _.role
    • Variables
    • Conditionals
    • Loops
  • Editor Extension Settings
    • baml.cliPath
    • baml.generateCodeOnSave
    • baml.enablePlaygroundProxy
    • baml.syncExtensionToGeneratorVersion
Help on Discord
LogoLogo
On this page
  • Usage
  • Field Assertion
  • Parameter Assertion
  • Block Assertion
Attributes

@assert

Was this page helpful?
Edit this page
Previous

@check

Next
Built with

The @assert attribute in BAML is used for strict validations. If a type fails an @assert validation, it will not be returned in the response, and an exception will be raised if it’s part of the top-level type.

Usage

Asserts can be named or unnamed.

Field Assertion

BAML
1class Foo {
2 // @assert will be applied to the field with the name "bar"
3 bar int @assert(between_0_and_10, {{ this > 0 and this < 10 }})
4}
BAML
1class Foo {
2 // @assert will be applied to the field with no name
3 bar int @assert({{ this > 0 and this < 10 }})
4}
BAML
1class MyClass {
2 // @assert will be applied to each element in the array
3 my_field (string @assert(is_valid_email, {{ this|regex_match("@") }}))[]
4}

Parameter Assertion

Asserts can also be applied to parameters.

BAML
1function MyFunction(x: int @assert(between_0_and_10, {{ this > 0 and this < 10 }})) {
2 client "openai/gpt-4o"
3 prompt #"Hello, world!"#
4}

Block Assertion

Asserts can be used in a block definition, referencing fields within the block.

BAML
1class Foo {
2 bar int
3 baz string
4 @@assert(baz_length_limit, {{ this.baz|length < this.bar }})
5}

See Jinja in Attributes for a longer description of the Jinja syntax available in asserts.