Block SDK Guide

This guide explains how to create new blocks for the AutoGPT Platform using the SDK pattern with advanced features.

Overview

Blocks are reusable components that perform specific tasks in AutoGPT workflows. They can integrate with external services, process data, or perform any programmatic operation.

Basic Structure

1. Create Provider Configuration

First, create a _config.py file to configure your provider using the ProviderBuilder:

from backend.sdk import BlockCostType, ProviderBuilder

# Simple API key provider
my_provider = (
    ProviderBuilder("my_provider")
    .with_api_key("MY_PROVIDER_API_KEY", "My Provider API Key")
    .with_base_cost(1, BlockCostType.RUN)
    .build()
)

For OAuth providers:

2. Create the Block Class

Create your block file (e.g., my_block.py):

Key Components Explained

Provider Configuration

The ProviderBuilder allows you to:

  • .with_api_key(): Add API key authentication

  • .with_oauth(): Add OAuth authentication

  • .with_base_cost(): Set resource costs for the block

  • .with_webhook_manager(): Add webhook support

  • .with_user_password(): Add username/password auth

Block Schema

  • Input/Output classes: Define the data structure using BlockSchema

  • SchemaField: Define individual fields with validation

  • CredentialsMetaInput: Special field for handling credentials

Block Implementation

  1. Unique ID: Generate using uuid.uuid4()

  2. Categories: Choose from BlockCategory enum (e.g., SEARCH, AI, PRODUCTIVITY)

  3. async run(): Main execution method that yields outputs

  4. Error handling: Error output pin is already defined on BlockSchemaOutput

Advanced Features

Testing

Add test configuration to your block:

OAuth Support

Create an OAuth handler in _oauth.py:

Webhook Support

Create a webhook manager in _webhook.py:

File Organization

Best Practices

  1. Error Handling: Use BlockInputError for validation failures and BlockExecutionError for runtime errors (import from backend.util.exceptions). These inherit from ValueError so the executor treats them as user-fixable. See Error Handling in new_blocks.md for details.

  2. Credentials: Use the provider's credentials_field() method

  3. Validation: Use SchemaField constraints (ge, le, min_length, etc.)

  4. Categories: Choose appropriate categories for discoverability

  5. Advanced Fields: Mark complex options as advanced=True

  6. Async Operations: Use async/await for I/O operations

  7. API Clients: Use Requests() from SDK or external libraries

  8. Testing: Include test inputs/outputs for validation

Common Patterns

Making API Requests

Multiple Auth Types

Testing Your Block

Integration Checklist

Example Blocks for Reference

  • Simple API: /backend/blocks/firecrawl/ - Basic API key authentication

  • OAuth + API: /backend/blocks/linear/ - OAuth and API key support

  • Webhooks: /backend/blocks/exa/ - Includes webhook manager

Study these examples to understand different patterns and approaches for building blocks.

Last updated

Was this helpful?