This file provides guidance to AI coding assistants when working with code in this repository.
Banks is a Python prompt programming language and templating system for LLM applications. It provides a Jinja2-based template engine with specialized extensions and filters for creating dynamic prompts, managing chat messages, handling multimodal content (images/audio/video/documents), and integrating with various LLM providers through LiteLLM.
# Most common commands
uv run pytest tests # Run unit tests
uv run ruff format --check && uv run ruff check . && uv run mypy --install-types --non-interactive src/banks && uv run pylint src/banks # Run all linting checks
uv run ruff format # Auto-format code
uv run pytest tests/test_foo.py # Run specific test file- Install dev dependencies:
uv sync --extra dev
- Run tests:
uv run pytest tests - Run tests with coverage:
uv run pytest --cov --cov-report=xml tests - Generate coverage report:
uv run pytest --cov --cov-report=xml tests && uv run coverage combine && uv run coverage report -m - Run specific test file:
uv run pytest tests/test_foo.py - Run e2e tests:
uv run pytest tests/e2e/(requires API keys)
- Format code:
uv run ruff format - Auto-fix lint issues:
uv run ruff check --fix - Check formatting:
uv run ruff format --check && uv run ruff check . - Run type checking:
uv run mypy --install-types --non-interactive src/banks - Run pylint:
uv run pylint src/banks - Run all lint checks:
uv run ruff format --check && uv run ruff check . && uv run mypy --install-types --non-interactive src/banks && uv run pylint src/banks
- Build docs:
uv run mkdocs build - Serve docs locally:
uv run mkdocs serve(available at http://127.0.0.1:8000/)
- Uses
uvfor dependency management and running tools - Install all dev dependencies:
uv sync --extra dev - Python 3.9+ supported (tested on 3.10-3.14)
Prompt Classes (src/banks/prompt.py):
BasePrompt: Base class with template rendering, metadata, versioning, and cachingPrompt: Synchronous prompt rendering withtext()andchat_messages()methodsAsyncPrompt: Asynchronous version (requiresBANKS_ASYNC_ENABLED=true)PromptRegistry: Protocol interface for prompt storage backends
Type System (src/banks/types.py):
ChatMessage: Core chat message structure with role and contentContentBlock: Handles different content types (text, image_url, audio, video, document) with optional cache controlTool: Function calling support with automatic schema generation from Python callablesCacheControl: Anthropic-style prompt caching metadata
Template Environment (src/banks/env.py):
- Global Jinja2 environment with Banks-specific extensions and filters
- Async support detection and configuration
- Custom template loader integration
Error Types (src/banks/errors.py):
MissingDependencyError: Optional dependencies not installedAsyncError: Asyncio support misconfigurationCanaryWordError: Canary word leaked (prompt injection detection)PromptNotFoundError: Prompt not found in registryInvalidPromptError: Invalid prompt formatLLMError: LLM provider errors
Chat Extension (src/banks/extensions/chat.py):
{% chat role="..." %}...{% endchat %}blocks for structured message creation- Automatic conversion to
ChatMessageobjects during rendering
Completion Extension (src/banks/extensions/completion.py):
{% completion model="..." %}...{% endcompletion %}for in-prompt LLM calls- Integrated with LiteLLM for multi-provider support
- Function calling support within completion blocks
Core Filters (src/banks/filters/):
image: Convert file paths/URLs/bytes to base64-encoded image content blocksaudio: Convert audio files to base64-encoded audio content blocksvideo: Convert video files to base64-encoded video content blocksdocument: Convert documents (PDF, TXT, HTML, CSS, XML, CSV, RTF, JS, JSON) to base64-encoded content blockscache_control: Add Anthropic cache control metadata to content blockstool: Convert Python callables to LLM function call schemaslemmatize: Text lemmatization using simplemma
Filter Pattern: Filters wrap content in <content_block> tags and are only useful within {% chat %} blocks.
Storage Backends (src/banks/registries/):
DirectoryTemplateRegistry: File system-based prompt storageFileTemplateRegistry: Single file-based storageRedisTemplateRegistry: Redis-backed storage for distributed scenarios- All registries implement the
PromptRegistryprotocol
Render Cache (src/banks/cache.py):
RenderCache: Protocol interface for caching rendered promptsDefaultCache: In-memory cache using pickle-serialized context as key- Prevents re-rendering identical template + context combinations
Config System (src/banks/config.py):
- Environment variable-based configuration with
BANKS_prefix BANKS_ASYNC_ENABLED: Enable async template rendering (must be set before import)BANKS_USER_DATA_PATH: Custom user data directory
- Templates parsed by Jinja2 environment with Banks extensions
- Chat blocks converted to JSON during rendering
chat_messages()parses JSON back toChatMessageobjects- Caching layer prevents re-rendering identical contexts
- Images/audio/video/documents converted to base64 during filter application
- Filters accept file paths, URLs, or raw bytes
- Content blocks maintain type safety and metadata
- Cache control integrated at content block level
- Python functions automatically converted to LLM schemas via introspection
- Docstring parsing for parameter descriptions
- Type annotations converted to JSON Schema
- Global environment state requires async decision at import time
BANKS_ASYNC_ENABLEDmust be set before importing banks modulesAsyncPromptprovidesawait-able rendering methods
@pytest.mark.e2e: End-to-end tests requiring external services@pytest.mark.redis: Tests requiring a running Redis instance
OPENAI_API_KEY: For OpenAI-based testsANTHROPIC_API_KEY: For Anthropic-based tests
- Test fixtures in
tests/data/(images, audio, video, PDFs) - Template examples in
tests/templates/
uv run pytest tests/test_image.py # Single file
uv run pytest tests/test_image.py::test_name # Single test
uv run pytest -k "image" # Tests matching pattern- Line length: 120 characters
- Use ruff for formatting and linting
- Imports sorted with
banksas first-party
- All public functions should have type annotations
- Use
from __future__ import annotationsfor forward references - MyPy strict mode enforced
- SPDX license headers in all source files
- Docstrings for public APIs
- Relative imports banned (use absolute
from banks.x import y)
The main exports from banks package:
from banks import Prompt, AsyncPrompt, ChatMessage, config, envCore (required):
jinja2: Core templating enginepydantic: Type validation and serializationgriffe: Code introspection utilitiesplatformdirs: Cross-platform data directory handlingfiletype: File type detection for multimodal contentdeprecated: Deprecation decorators
Optional:
litellm: Multi-provider LLM integration (banks[all])redis: Redis registry backend (banks[all])simplemma: Lemmatization filter (dev dependency)
- test.yml: Runs tests on Python 3.10-3.14
- docs.yml: Builds and deploys documentation
- release.yml: Handles package releases
Follow conventional commit prefixes for PR titles:
fix:- Bug fixesfeat:- New featureschore:- Maintenancedocs:- Documentationrefactor:- Code refactoringtest:- Test additions/changes