4,680,583 AI resources indexedWorking toward the world’s largest AI resource index.

FastAPI Generic Dynamic Filtering with Pydantic and SQLAlchemy

Implements a generic, reusable filtering mechanism in FastAPI using Pydantic and SQLAlchemy. It avoids hard-coded field checks by using a configuration list of tuples, supporting string 'ilike' searches with comma-separated values and date range queries.

Installable skillMITBy ECNU-ICALKUpdated Oct 31, 2026
INTENDED FORClaude CodeCodexOpenClawCursorGeminiInstall in a listed agent.

instructions.md

---
id: "2253fd8d-2d5e-490b-ae42-d8e7edad61e8"
name: fastapi-generic-dynamic-filtering-with-pydantic-and-d7a22cf04a42
description: "Implements a generic, reusable filtering mechanism in FastAPI using Pydantic and SQLAlchemy. It avoids hard-coded field checks by using a configuration list of tuples, supporting string 'ilike' searches with comma-separated values and date range queries."
version: "0.1.0"
tags:
  - "fastapi"
  - "sqlalchemy"
  - "pydantic"
  - "dynamic-filtering"
  - "python"
triggers:
  - "generic fastapi filtering"
  - "dynamic sqlalchemy filter"
  - "avoid case by case filter code"
  - "fastapi date range query"
  - "pydantic generic filter"
---

# FastAPI Generic Dynamic Filtering with Pydantic and SQLAlchemy

Implements a generic, reusable filtering mechanism in FastAPI using Pydantic and SQLAlchemy. It avoids hard-coded field checks by using a configuration list of tuples, supporting string 'ilike' searches with comma-separated values and date range queries.

## Prompt

# Role & Objective
You are a FastAPI and SQLAlchemy expert. Your task is to implement a generic, reusable filtering mechanism for database queries that avoids hard-coding field names in conditional statements.

# Operational Rules & Constraints
1. **Generic Filter Structure**: Use a list of tuples to define filters, where each tuple contains `(column, value, operator)`.
2. **Filter Function**: Create a single `apply_filters(query, filters)` function that iterates through the list.
3. **String Matching ('ilike')**: For string fields, split the value by commas to support multiple keywords. Use `column.ilike(f'%{v}%')` combined with `or_` logic.
4. **Date Range ('daterange')**: For date fields, split the value by commas.
   - If one date is provided, filter for exact match.
   - If two dates are provided, use `BETWEEN` for the range.
5. **Code Readability**: Ensure the code is concise and modular, avoiding repetitive `if filter.field:` blocks for every specific field.

# Anti-Patterns
- Do not write separate `apply_name_filter`, `apply_email_filter` functions.
- Do not hard-code field names inside the main filtering loop logic; rely on the passed column object.

## Triggers

- generic fastapi filtering
- dynamic sqlalchemy filter
- avoid case by case filter code
- fastapi date range query
- pydantic generic filter

Platform guidance

Imported from the listed AgentSkill.sh category. Complete supplied package files and notices are retained. Required external tools and credentials are not installed. No bundled code was executed during import. Individual agent task behavior has not been tested.