Skip to main content

1. What is an AI Block?

An AI block is a special type of block in a Mage pipeline that uses a Large Language Model (LLM) to:
  • Generate text or code
  • Return structured data in a defined schema
  • Trigger other pipelines as tools
  • Validate outputs before passing them downstream
AI blocks are configured in YAML and can be designed to:
  • Write and validate executable code
  • Produce structured outputs for downstream blocks
  • Decide which other pipelines to trigger and pass them variables
They integrate directly with Mage’s orchestration engine, allowing AI-generated outputs to be chained together, validated, and reused.

2. How to Use an AI Block

  1. Add a new block in your Mage pipeline and set its type to ai.
  2. Write a prompt — the main instruction for the AI.
  3. Optionally define output settings:
    • Code generation (output.code)
    • Structured JSON schema (output.format)
  4. (Optional) Configure tools to allow the AI to trigger other pipelines.
  5. Run the pipeline — Mage will send your prompt and configuration to the AI model, validate the output (if specified), and pass it to the next blocks.
Minimal example:

3. AI Block Configuration

3.1 Basic Structure


3.2 Fields

prompt (Required)

The instruction to send to the AI model.
  • Supports multi-line strings with |.
  • Example:

output

Controls how the AI’s response is validated and structured.
  1. Code Output
  1. Structured Format Output
  1. Unstructured Output

tools

Lets the AI trigger other pipelines as part of execution.

4. JSON Schema Support for Structured Outputs

The output.format and variables fields use JSON Schema draft-07 (subset) to define:
  • type — string, number, integer, boolean, array, object
  • description — field description
  • enum — allowed values
  • items — item schema for arrays
  • minItems, maxItems — constraints for arrays
  • pattern — regex validation for strings
  • required — list of required keys
  • oneOf, anyOf, allOf — branching validation
  • $ref, $defs — recursive or modular schemas
Example:

5. Best Practices

  • Use output.code only when expecting executable code.
  • Use output.format for predictable structured outputs.
  • Avoid defining both code and format in the same block.
  • Use multi-line prompts with | for readability.
  • Only include fields you need — minimal configs run faster.
  • Always include a validation prompt for critical outputs.
  • When chaining pipelines, define variables clearly for tool execution.

6. Example Configurations

Minimal AI Block
AI Block with Structured Output
AI Block Triggering Another Pipeline