Skip to content

Add fetch_db_schema to DatabaseTool for compact schema introspection#216

Merged
scottpersinger merged 1 commit intosupercog-ai:mainfrom
vivekd16:main
Jun 10, 2025
Merged

Add fetch_db_schema to DatabaseTool for compact schema introspection#216
scottpersinger merged 1 commit intosupercog-ai:mainfrom
vivekd16:main

Conversation

@vivekd16
Copy link
Contributor

@vivekd16 vivekd16 commented Jun 9, 2025

This PR adds a fetch_db_schema method to the DatabaseTool class, enabling agents to retrieve a compact, human-readable schema summary for all or selected tables in a connected SQL database. This allows agents to preload schema information without requiring runtime introspection via SQL queries.

Features

  • New method: fetch_db_schema(thread_context, tables=None)
    • Returns a string like:
      table "users":
        id int
        name varchar
        email varchar
      table "accounts":
        ...
      
    • If tables is provided, only those tables are included; otherwise, all tables are listed.
  • Tool registration: The new method is registered in get_tools() so it is available to agents and tool discovery.
  • Error handling: If a table cannot be introspected, the error is included in the output for that table.

Why?

  • Allows agents to preload schema context for better SQL generation and reasoning.
  • Reduces the need for repeated schema introspection queries at runtime.
  • Supports workflows where schema is needed as part of agent memory or prompt context.

Tests

  • Added tests/test_database_tool.py with tests for both all-tables and single-table schema fetches using a sample SQLite database.
  • Tests verify that the output format matches the requested compact, readable style.

Example Usage

dbtool = DatabaseTool()
agent = Agent(..., tools=[dbtool], memories=[dbtool.fetch_db_schema("accounts", "users", "sales")])

Copy link
Collaborator

@scottpersinger scottpersinger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Thanks @vivekd16 - this looks great.

@scottpersinger scottpersinger merged commit 25c32c1 into supercog-ai:main Jun 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants