Skip to main content
skill CX Codex

python-error-handling — Claude Code skill: how to install

Guides the coding assistant in Python error handling patterns, focusing on early boundary validation and chained exception traces.

Install into Claude Code · Plugin marketplace All methods and platforms ↓
/plugin marketplace add wshobson/agents
/plugin install python-development@claude-code-workflows
Open on GitHub

What python-error-handling does

The skill guides an AI assistant in handling failures across Python code. It enforces early input validation at function boundaries, stopping execution before expensive calls run. Raw strings from user input are parsed into domain types like Enum to keep internal operations strictly typed.

For complex payloads, the pattern prescribes BaseModel with field_validator methods. Generic Exception raises without context are discarded in favor of standard built-ins such as ValueError or KeyError. In batch processing, single item failures are isolated so that the entire batch does not abort. Chained exceptions preserve the complete traceback via raise ... from e.

Using the structured models requires pydantic in your environment. Additional worked examples are stored in references/details.md.

What it helps with

  • validate function inputs early before executing expensive operations
  • convert raw input strings into typed Enum members
  • structure complex validation logic using Pydantic models
  • preserve debugging tracebacks by chaining exceptions with raise ... from e
  • track partial failures in batch operations without aborting the entire run

Example prompts for the agent

  • Add input validation and early ValueError checks to process_order
  • Create a Pydantic model for user registration with email format validation
  • Implement partial failure handling for the batch export loop so one error does not halt the job

How to install python-error-handling

1Plugin marketplace

/plugin marketplace add wshobson/agents
/plugin install python-development@claude-code-workflows

This installs the whole python-development plugin with all its skills

2Skills CLI (Vercel)

npx skills add wshobson/agents --skill python-error-handling -a claude-code

3Manually: put the files in place

~/.claude/skills/python-error-handling/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/python-error-handling/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about python-error-handling

How to install python-error-handling in Claude Code?

Run /plugin marketplace add wshobson/agents followed by /plugin install python-development@claude-code-workflows.

What does python-error-handling do?

It guides the assistant in applying fail-fast input validation, mapping errors to standard Python exceptions, chaining tracebacks, and handling partial failures in batch operations.

Is Pydantic required to use this skill?

Basic checks rely strictly on standard Python exceptions and enums. Pydantic is only necessary if you choose to implement complex validation models with BaseModel and field_validator.

Source text

SKILL.md · MIT
---
name: python-error-handling
description: Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.
---

# Python Error Handling

Build robust Python applications with proper input validation, meaningful exceptions, and graceful failure handling. Good error handling makes debugging easier and systems more reliable.

## When to Use This Skill

- Validating user input and API parameters
- Designing exception hierarchies for applications
- Handling partial failures in batch operations
- Converting external data to domain types
- Building user-friendly error messages
- Implementing fail-fast validation patterns

## Core Concepts

### 1. Fail Fast

Validate inputs early, before expensive operations. Report all validation errors at once when possible.

### 2. Meaningful Exceptions

Use appropriate exception types with context. Messages should explain what failed, why, and how to fix it.

### 3. Partial Failures

In batch operations, don't let one failure abort everything. Track successes and failures separately.

### 4. Preserve Context

Chain exceptions to maintain the full error trail for debugging.

## Quick Start

```python
def fetch_page(url: str, page_size: int) -> Page:
    if not url:
        raise ValueError("'url' is required")
    if not 1 <= page_size <= 100:
        raise ValueError(f"'page_size' must be 1-100, got {page_size}")
    # Now safe to proceed...
```

## Fundamental Patterns

### Pattern 1: Early Input Validation

Validate all inputs at API boundaries before any processing begins.

```python
def process_order(
    order_id: str,
    quantity: int,
    discount_percent: float,
) -> OrderResult:
    """Process an order with validation."""
    # Validate required fields
    if not order_id:
        raise ValueError("'order_id' is required")

    # Validate ranges
    if quantity <= 0:
        raise ValueError(f"'quantity' must be positive, got {quantity}")

    if not 0 <= discount_percent <= 100:
        raise ValueError(
            f"'discount_percent' must be 0-100, got {discount_percent}"
        )

    # Validation passed, proceed with processing
    return _process_validated_order(order_id, quantity, discount_percent)
```

### Pattern 2: Convert to Domain Types Early

Parse strings and external data into typed domain objects at system boundaries.

```python
from enum import Enum

class OutputFormat(Enum):
    JSON = "json"
    CSV = "csv"
    PARQUET = "parquet"

def parse_output_format(value: str) -> OutputFormat:
    """Parse string to OutputFormat enum.

    Args:
        value: Format string from user input.

    Returns:
        Validated OutputFormat enum member.

    Raises:
        ValueError: If format is not recognized.
    """
    try:
        return OutputFormat(value.lower())
    except ValueError:
        valid_formats = [f.value for f in OutputFormat]
        raise ValueError(
            f"Invalid format '{value}'. "
            f"Valid options: {', '.join(valid_formats)}"
        )

# Usage at API boundary
def export_data(data: list[dict], format_str: str) -> bytes:
    output_format = parse_output_format(format_str)  # Fail fast
    # Rest of function uses typed OutputFormat
    ...
```

### Pattern 3: Pydantic for Complex Validation

Use Pydantic models for structured input validation with automatic error messages.

```python
from pydantic import BaseModel, Field, field_validator

class CreateUserInput(BaseModel):
    """Input model for user creation."""

    email: str = Field(..., min_length=5, max_length=255)
    name: str = Field(..., min_length=1, max_length=100)
    age: int = Field(ge=0, le=150)

    @field_validator("email")
    @classmethod
    def validate_email_format(cls, v: str) -> str:
        if "@" not in v or "." not in v.split("@")[-1]:
            raise ValueError("Invalid email format")
        return v.lower()

    @field_validator("name")
    @classmethod
    def normalize_name(cls, v: str) -> str:
        return v.strip().title()

# Usage
try:
    user_input = CreateUserInput(
        email="[email protected]",
        name="john doe",
        age=25,
    )
except ValidationError as e:
    # Pydantic provides detailed error information
    print(e.errors())
```

### Pattern 4: Map Errors to Standard Exceptions

Use Python's built-in exception types appropriately, adding context as needed.

| Failure Type | Exception | Example |
|--------------|-----------|---------|
| Invalid input | `ValueError` | Bad parameter values |
| Wrong type | `TypeError` | Expected string, got int |
| Missing item | `KeyError` | Dict key not found |
| Operational failure | `RuntimeError` | Service unavailable |
| Timeout | `TimeoutError` | Operation took too long |
| File not found | `FileNotFoundError` | Path doesn't exist |
| Permission denied | `PermissionError` | Access forbidden |

```python
# Good: Specific exception with context
raise ValueError(f"'page_size' must be 1-100, got {page_size}")

# Avoid: Generic exception, no context
raise Exception("Invalid parameter")
```

## Detailed worked examples and patterns

Detailed sections (starting with `## Advanced Patterns`) live in `references/details.md`. Read that file when the navigation summary above is insufficient.

## Best Practices Summary

1. **Validate early** - Check inputs before expensive operations
2. **Use specific exceptions** - `ValueError`, `TypeError`, not generic `Exception`
3. **Include context** - Messages should explain what, why, and how to fix
4. **Convert types at boundaries** - Parse strings to enums/domain types early
5. **Chain exceptions** - Use `raise ... from e` to preserve debug info
6. **Handle partial failures** - Don't abort batches on single item errors
7. **Use Pydantic** - For complex input validation with structured errors
8. **Document failure modes** - Docstrings should list possible exceptions
9. **Log with context** - Include IDs, counts, and other debugging info
10. **Test error paths** - Verify exceptions are raised correctly

© Seth Hobson · MIT · wshobson/agents

Similar skills

Everything for Backend and APIs →

From the same set342

The whole set →
python-project-structure This skill organizes Python modules, public APIs, tests, and directory layouts. Use it when starting a project, reshaping a codebase, or building a reusable package. CX Codex 14,768installs k8s-security-policies This skill helps define Kubernetes security policies for network isolation, RBAC, and pod security standards. Use it when securing a cluster. CX Codex 14,692installs accessibility-compliance This skill guides accessibility audits and interface work involving keyboard navigation, screen readers, and forms. Use it when building against WCAG 2.2. CX Codex 14,596installs monorepo-management This skill guides monorepo setup with Turborepo, Nx, and pnpm workspaces. Use it for builds, shared dependencies, CI/CD, and package releases. CX Codex 14,548installs rag-implementation This skill helps build RAG systems with vector search and LLM answers grounded in external documents. Use it for document Q&A and knowledge assistants. CX Codex 13,906installs deployment-pipeline-design Designs staged CI/CD pipelines with approvals, security checks and rollout strategies. Use it for canary, blue-green and rollback planning. CX Codex 13,758installs changelog-automation This skill helps generate changelogs and release notes from commits, PRs, and releases. Use it when setting up release workflows or shared commit rules. CX Codex 13,733installs visual-design-foundations This skill helps define typography, color, spacing, and icon rules for a project. Use it when setting up design tokens or bringing an interface into visual consistency. CX Codex 13,566installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor