Skip to content

API Reference parrot.bots.database.agent

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

parrot.bots.database.agent

View source

DatabaseAgent — LLM-backed unified agent with structured output.

Inherits from BasicAgent, delegates all database operations to toolkits, and uses QueryResponse structured output for every ask() call.

Alias AIMessage

Re-exported from parrot.models.AIMessage.

Alias AbstractStore

Re-exported from parrot.stores.abstract.AbstractStore.

Alias BasicAgent

Re-exported from parrot.bots.agent.BasicAgent.

Alias CacheManager

Re-exported from parrot.bots.database.cache.CacheManager.

Alias CachePartitionConfig

Re-exported from parrot.bots.database.cache.CachePartitionConfig.

Alias CompletionUsage

Re-exported from parrot.models.CompletionUsage.

Class DatabaseAgent

Unified database agent backed by BasicAgent + QueryResponse structured output.

Inherits composable prompt layers, LLM lifecycle, and tool management from BasicAgent. Database toolkits handle actual DB I/O; the internal DatabaseAgentToolkit exposes formatting helpers as LLM tools.

Parameter Type Default Description
name str 'DatabaseAgent' Agent display name.
toolkits Optional[List[DatabaseToolkit]] None List of DatabaseToolkit instances.
default_user_role UserRole UserRole.DATA_ANALYST Fallback role when none is provided/inferred.
vector_store Optional[AbstractStore] None Optional vector store for cache similarity search.
redis_url Optional[str] None Optional Redis URL for cache persistence.
retry_config Optional[QueryRetryConfig] None Optional query retry configuration.
cache_ttl_by_completeness Optional[Dict[int, int]] None
schema_plane Any None Optional schema plane service or directory path.
**kwargs Any {} Forwarded to BasicAgent.__init__.

Attributes:

Attribute Type Description
max_tokens int
toolkits List[DatabaseToolkit]
query_router Optional[SchemaQueryRouter]

Method DatabaseAgent.configure

Configure the agent: create cache partitions, start toolkits, register tools with the router, and instantiate the internal toolkit.

Validation steps (FEAT-172) run after all toolkits are started but before self._internal_toolkit is assigned:

  1. Prefix presence — raises ValueError if any toolkit's tool_prefix is None or ""; every database toolkit must declare a non-empty prefix.
  2. Prefix shape — raises ValueError if any toolkit's tool_prefix does not match ^[A-Za-z][A-Za-z0-9_]*$; a non-identifier-safe prefix would produce tool names rejected by LLM providers.
  3. Collision detection — raises ValueError if two toolkits expose the same fully-qualified tool name (detected via list_tool_names()); the error names both conflicting classes.

On any validation failure self._internal_toolkit remains None. Callers must construct a fresh DatabaseAgent instance after a ValueError — a failed agent must not be re-used.

configure(app: Any = None) -> None

Parameter Type Default Description
app Any None Optional application context (unused, for API compatibility).

Raises:

  • ValueError — If a toolkit's tool_prefix is empty or None.
  • ValueError — If a toolkit's tool_prefix is not identifier-safe.
  • ValueError — If two toolkits register the same fully-qualified tool name.

Method DatabaseAgent.cleanup

Stop all toolkits, close the cache manager, then run base cleanup.

The database-specific resources (toolkit asyncdb pools + the CacheManager's Redis pool and owned vector-store engine) are torn down first, then super().cleanup() releases the resources owned by the base agent — LLM client/session, self.store, knowledge bases, and MCP client sessions. Each phase is isolated so a failure in one does not prevent the others from running (teardown must not raise).

cleanup() -> None

Method DatabaseAgent.create_system_prompt

Build the system prompt using the database-specific PromptBuilder layers.

Delegates to the parent's implementation when available; otherwise builds the prompt directly from _prompt_builder (test-stub compatibility).

create_system_prompt(**kwargs: Any) -> str

Parameter Type Default Description
**kwargs Any {} Template variables forwarded to the prompt builder (e.g., database, intent, schema_summary).

Returns: str — Rendered system prompt string.

Method DatabaseAgent.get_default_components

Return default output components for a user role.

Delegates to the module-level helper so the result is identical.

get_default_components(user_role: UserRole) -> OutputComponent

Parameter Type Default Description
user_role UserRole The role to look up.

Returns: OutputComponent — OutputComponent flags appropriate for that role.

Method DatabaseAgent.ask

Process a database query using registered toolkits and the LLM.

Role resolution (three-tier):

  1. Explicit user_role parameter
  2. Inferred from query intent by the router
  3. default_user_role fallback

ask(question: str, user_role: Optional[UserRole] = None, database: Optional[str] = None, context: Optional[str] = None, output_components: Optional[Union[str, OutputComponent]] = None, output_format: Optional[Any] = None, session_id: Optional[str] = None, user_id: Optional[str] = None, structured_output: Optional[Any] = None, output_mode: Optional[OutputMode] = None, **kwargs: Any) -> AIMessage

Parameter Type Default Description
question str Natural-language query or SQL. Named question to honor the universal AbstractBot.ask contract (the HTTP handler and every sibling bot call ask(question=...)); aliased internally to query for the SQL-centric logic below.
user_role Optional[UserRole] None Explicit role override (highest priority).
database Optional[str] None Explicit toolkit identifier.
context Optional[str] None Additional context string injected into the prompt.
output_components Optional[Union[str, OutputComponent]] None Desired output components (flags or comma-separated string).
output_format Optional[Any] None Unused; reserved for future formatting options.
session_id Optional[str] None Session identifier for memory tracking.
user_id Optional[str] None User identifier.
structured_output Optional[Any] None Override the default QueryResponse structured output.
output_mode Optional[OutputMode] None Override the default OutputMode.SQL_ANALYSIS envelope mode. Pass OutputMode.STRUCTURED_TABLE to route the QueryDataset and QueryResponse.explanation through StructuredTableRenderer instead of the SQL artifact card path.
**kwargs Any {} Forwarded to the LLM client.

Returns: AIMessage — AIMessage with the formatted response and unpacked QueryResponse.

Method DatabaseAgent.conversation

Conversation method — delegates to ask().

conversation(question: str, **kwargs: Any) -> AIMessage

Parameter Type Default Description
question str
**kwargs Any {}

Returns: AIMessage

Method DatabaseAgent.invoke

Invoke method — delegates to ask().

invoke(question: str, **kwargs: Any) -> AIMessage

Parameter Type Default Description
question str
**kwargs Any {}

Returns: AIMessage

Method DatabaseAgent.ask_stream

Streaming ask — yields single response (streaming not yet implemented).

ask_stream(question: str, **kwargs: Any)

Parameter Type Default Description
question str
**kwargs Any {}

Alias DatabaseAgentToolkit

Re-exported from parrot.bots.database.toolkits.DatabaseAgentToolkit.

Alias DatabaseToolkit

Re-exported from parrot.bots.database.toolkits.base.DatabaseToolkit.

Alias OutputComponent

Re-exported from parrot.bots.database.models.OutputComponent.

Alias OutputMode

Re-exported from parrot.models.outputs.OutputMode.

Alias PromptBuilder

Re-exported from parrot.bots.prompts.builder.PromptBuilder.

Alias QueryResponse

Re-exported from parrot.bots.database.models.QueryResponse.

Alias QueryRetryConfig

Re-exported from parrot.bots.database.retries.QueryRetryConfig.

Alias RetryContext

Re-exported from parrot.bots.database.retries.RetryContext.

Alias SchemaQueryRouter

Re-exported from parrot.bots.database.router.SchemaQueryRouter.

Alias StructuredOutputConfig

Re-exported from parrot.models.outputs.StructuredOutputConfig.

Alias UserRole

Re-exported from parrot.bots.database.models.UserRole.

Alias attach_structured_artifact

Re-exported from parrot.outputs.a2ui.artifacts.attach_structured_artifact.

Alias components_from_string

Re-exported from parrot.bots.database.models.components_from_string.

Alias get_default_components

Re-exported from parrot.bots.database.models.get_default_components.

Alias with_call_identity

Re-exported from parrot.bots.base.with_call_identity.

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