python-error-handling

Implements Python input validation, exception hierarchies, and partial failure handling patterns.

Updated Apr 13, 2026
One-click install
npx skills add https://github.com/scoots31/engineering-playbook --skill python-error-handling-scoots31
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-error-handling
Source: https://github.com/scoots31/engineering-playbook/tree/main/references/python-error-handling
Command: npx skills add https://github.com/scoots31/engineering-playbook --skill python-error-handling-scoots31

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pydantic, httpx.

What problem does it solve? Python applications often fail unpredictably due to missing input validation, generic exceptions without context, and batch operations that abort entirely when a single item fails. This Skill provides concrete patterns for building robust error handling that makes debugging easier and systems more resilient. ## Core Features & Use Cases - Input Validation Patterns: Fail-fast validation at API boundaries, early conversion of strings to domain types like enums, and Pydantic models for structured validation with automatic error messages. - Exception Design: Custom exception hierarchies carrying structured context (status codes, retry delays), proper mapping to built-in exception types, and exception chaining with raise ... from e to preserve debug trails. - Partial Failure Handling: Batch processing patterns that track successes and failures per item instead of aborting, plus progress callbacks for long-running operations. - Use Case: When building an API endpoint that processes a batch of uploaded records, use these patterns to validate each record, return detailed per-item errors, and let valid records succeed even when some fail. ## Quick Start Show me how to validate function inputs and handle partial failures when processing a batch of records in Python.

Frequently Asked Questions about python-error-handling

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I validate function inputs in Python?

Validate inputs at the start of the function before any processing, raising ValueError with messages that explain what failed and the valid range. For complex structured input, use Pydantic models with Field constraints and custom validators.

How to handle partial failures in Python batch processing?

Process each item inside a try-except block and collect results into separate succeeded and failed dictionaries keyed by index. Return a result object exposing success and failure counts so callers can log failures without aborting the entire batch.

Should I use Pydantic or manual validation for Python APIs?

Use Pydantic when validating structured input with multiple fields, since it provides automatic type coercion and detailed error reports. Use manual checks for simple functions with one or two parameters where a full model adds unnecessary overhead.

What is exception chaining in Python and when should I use it?

Exception chaining uses raise NewError(...) from e to wrap a low-level exception in a domain-specific one while preserving the original traceback. Use it when converting library errors like httpx exceptions into your application's own exception types.

Which built-in Python exception should I raise for invalid input?

Raise ValueError for invalid parameter values, TypeError for wrong types, KeyError for missing dictionary keys, and TimeoutError for exceeded time limits. Avoid generic Exception since it hides the failure category from callers and debugging tools.