Skip to content

Validation Utilities

The validation module provides utility functions for validating tool schemas and parameters in the Bedrock Swarm framework. These functions ensure data integrity and type safety across tool operations.

Module Documentation

Tool validation utilities.

Functions

validate_tool_schema(tool_name: str, schema: Dict[str, Any]) -> None

Validate tool schema.

PARAMETER DESCRIPTION
tool_name

Name of the tool

TYPE: str

schema

Tool schema

TYPE: Dict[str, Any]

RAISES DESCRIPTION
ValueError

If schema is invalid

Source code in src/bedrock_swarm/tools/validation.py
def validate_tool_schema(tool_name: str, schema: Dict[str, Any]) -> None:
    """Validate tool schema.

    Args:
        tool_name: Name of the tool
        schema: Tool schema

    Raises:
        ValueError: If schema is invalid
    """
    if schema["name"] != tool_name:
        raise ValueError("Schema name must match tool name")

validate_tool_parameters(schema: Dict[str, Any], **kwargs: Any) -> None

Validate parameters against tool schema.

PARAMETER DESCRIPTION
schema

Tool schema

TYPE: Dict[str, Any]

**kwargs

Parameters to validate

TYPE: Any DEFAULT: {}

RAISES DESCRIPTION
ValueError

If parameters are invalid

Source code in src/bedrock_swarm/tools/validation.py
def validate_tool_parameters(schema: Dict[str, Any], **kwargs: Any) -> None:
    """Validate parameters against tool schema.

    Args:
        schema: Tool schema
        **kwargs: Parameters to validate

    Raises:
        ValueError: If parameters are invalid
    """
    param_schema = schema["parameters"]

    try:
        jsonschema.validate(instance=kwargs, schema=param_schema)
    except jsonschema.exceptions.ValidationError as e:
        error_str = str(e)
        if "required" in error_str:
            raise ValueError("Missing required parameter") from e
        elif "minItems" in error_str:
            raise ValueError("Array must have at least 1 item") from e
        elif "type" in error_str:
            raise ValueError("Invalid parameter type") from e
        else:
            raise ValueError(f"Invalid parameters: {error_str}") from e

Features

The validation utilities support:

  1. Tool Schema Validation:
  2. Validates tool name matches schema
  3. Ensures schema structure is correct
  4. Validates required fields

  5. Parameter Validation:

  6. JSON Schema validation
  7. Required parameter checks
  8. Type validation
  9. Array validation
  10. Custom validation rules

Usage Examples

from bedrock_swarm.tools.validation import validate_tool_schema, validate_tool_parameters

# Validate tool schema
schema = {
    "name": "my_tool",
    "parameters": {
        "type": "object",
        "properties": {
            "input": {"type": "string"},
            "count": {"type": "integer", "minimum": 0}
        },
        "required": ["input"]
    }
}

# Validate schema name matches tool name
validate_tool_schema("my_tool", schema)

# Validate parameters against schema
params = {
    "input": "test",
    "count": 5
}
validate_tool_parameters(schema, **params)

Error Handling

The validation utilities handle:

  1. Schema validation errors:
  2. Tool name mismatches
  3. Invalid schema structure
  4. Missing required fields

  5. Parameter validation errors:

  6. Missing required parameters
  7. Invalid parameter types
  8. Array validation failures
  9. Custom validation failures

Implementation Details

The validation system uses:

  1. JSON Schema validation through jsonschema library
  2. Detailed error messages for debugging
  3. Type checking and validation
  4. Array validation support
  5. Custom validation rules