Skip to content

API Reference parrot_tools.multidb

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot_tools.multidb

View source

Multi-Tier Schema Metadata Caching System for AI-Parrot DatabaseTool

This system implements intelligent schema caching with three tiers:

  1. In-memory cache for frequently accessed tables
  2. Vector database for semantic discovery of related tables
  3. Direct database extraction as last resort

The key insight: 90% of queries hit the same 10% of tables, so we optimize for this common case while gracefully handling discovery of new tables.

Alias AbstractTool

Re-exported from parrot_tools.abstract.AbstractTool.

Class EnhancedDatabaseTool

Enhanced DatabaseTool with intelligent multi-tier schema caching.

This version integrates the sophisticated caching system to provide lightning-fast schema access and intelligent query context building.

Parameter Type Default Description
vector_store Optional[AbstractStore] None
**kwargs {}

Class MetadataFormat

Supported metadata formats for schema representation.

Class SchemaMetadataCache

Multi-tier caching system for database schema metadata.

This class orchestrates the three-tier caching strategy: Tier 1: In-memory LRU cache for hot tables Tier 2: Vector store for semantic discovery Tier 3: Direct database extraction

The cache learns from usage patterns and optimizes for common access patterns.

Parameter Type Default Description
vector_store Optional[AbstractStore] None Vector database for semantic schema search
memory_cache_size int 100 Maximum number of tables to keep in memory
cache_ttl_hours int 24 How long to keep cached metadata valid
background_update bool True Whether to update vector store in background

Attributes:

Attribute Type Description
memory_cache OrderedDict[str, TableMetadata]
pending_updates Set[str]

Method SchemaMetadataCache.get_table_metadata

Get table metadata using the three-tier caching strategy.

This is the main entry point that implements the intelligent caching logic:

  1. Check in-memory cache first (fastest)
  2. Fall back to vector store semantic search
  3. Extract directly from database if needed
  4. Update higher tiers with new information

get_table_metadata(schema_name: str, table_name: str, database_type: str, database_extractor_func: Optional[callable] = None) -> Optional[TableMetadata]

Parameter Type Default Description
schema_name str
table_name str
database_type str
database_extractor_func Optional[callable] None

Returns: Optional[TableMetadata]

Method SchemaMetadataCache.get_context_for_query

Build comprehensive LLM context for a set of tables.

This method orchestrates the retrieval of multiple table metadata and formats it into a cohesive context for LLM query generation.

get_context_for_query(table_names: List[str], schema_name: str = 'public', database_type: str = 'postgresql', format_type: MetadataFormat = MetadataFormat.YAML_OPTIMIZED, database_extractor_func: Optional[callable] = None) -> str

Parameter Type Default Description
table_names List[str]
schema_name str 'public'
database_type str 'postgresql'
format_type MetadataFormat MetadataFormat.YAML_OPTIMIZED
database_extractor_func Optional[callable] None

Returns: str

Method SchemaMetadataCache.cleanup

Clean up background tasks and resources.

cleanup()

Method SchemaMetadataCache.get_cache_stats

Get caching statistics for monitoring and debugging.

get_cache_stats() -> Dict[str, Any]

Returns: Dict[str, Any]

Class TableMetadata

Optimized table metadata structure designed for both caching efficiency and LLM comprehension. This format balances completeness with conciseness.

Parameter Type Default Description
table_name str
schema_name str
database_type str
columns List[Dict[str, Any]]
primary_keys List[str]
foreign_keys List[Dict[str, str]]
indexes List[Dict[str, Any]]
description Optional[str] None
business_purpose Optional[str] None
common_joins List[str] None
sample_data Optional[Dict[str, List]] None
row_count_estimate Optional[int] None
last_updated datetime None
access_frequency int 0
cache_timestamp datetime None

Fields:

Field Type Default Description
table_name str
schema_name str
database_type str
columns List[Dict[str, Any]]
primary_keys List[str]
foreign_keys List[Dict[str, str]]
indexes List[Dict[str, Any]]
description Optional[str] None
business_purpose Optional[str] None
common_joins List[str] None
sample_data Optional[Dict[str, List]] None
row_count_estimate Optional[int] None
last_updated datetime None
access_frequency int 0
cache_timestamp datetime None

Method TableMetadata.to_llm_context

Convert table metadata to LLM-friendly format.

This is a critical method - the format needs to be:

  1. Concise enough to fit in LLM context windows
  2. Rich enough to generate accurate queries
  3. Structured enough for reliable parsing

to_llm_context(format_type: MetadataFormat = MetadataFormat.YAML_OPTIMIZED) -> str

Parameter Type Default Description
format_type MetadataFormat MetadataFormat.YAML_OPTIMIZED

Returns: str

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally