Skip to content

SQL Decorator created for TABLE_SCHEMA creation#11

Open
ojasaklechat41 wants to merge 8 commits into
think41:devfrom
ojasaklechat41:main
Open

SQL Decorator created for TABLE_SCHEMA creation#11
ojasaklechat41 wants to merge 8 commits into
think41:devfrom
ojasaklechat41:main

Conversation

@ojasaklechat41
Copy link
Copy Markdown

@ojasaklechat41 ojasaklechat41 commented Jun 9, 2025

Summary

This PR implements the support for Schema Generation ( #6 ). Now instead of manually writing the own " Create table " command. A new decorator " class SQLTableSchemaDecorator " is created which can be added about the functions signature just like we implement any api using foundation sql.

Closes #6

Implementation

from foundation_sql.query import SQLTableSchemaDecorator

class User(BaseModel):
    id: str
    name: str
    email: str
    role: UserRole
    created_at: Optional[datetime] = None

decorator = SQLTableSchemaDecorator(
            api_key=self.api_key,
            base_url=self.base_url,
            model=self.model,
            cache_dir=perf_cache_dir
        )

@decorator
def user_table(user: User) -> str:
    """Generate schema for user table."""
   pass
   
TABLE_SCHEMA = user_table()

@anshum4n-git
Copy link
Copy Markdown
Collaborator

Very interesting idea and implementation. Here are a few comments / observations.

  1. Can we do the decorator on the class itself. Feels cleaner. e.g.

@schema_decorator
class User(BaseModel):

  1. Lets follow the convention of keeping the prompts in a file (instead of .py). And then provide a way for user to override e.g.
    SQLTableSchemaDecorator(
    ... schema_prompt= OR schema_prompt=
    )

  2. In-fact, following up on the previous comment, it might be a good idea to move prompts into jinja templates - so the function and object specifications can be passed to it, instead of constructing the prompt in prompt.py with string concat.

@ojasaklechat41
Copy link
Copy Markdown
Author

@anshum4n-git for the 1st point if we add the decorator above the class we might have to get the schema like

TABLE_SCHEMA = User.get_sql_schema() 

while if we define a function we could just use

TABLE_SCHEMA = user_table()

@ojasaklechat41
Copy link
Copy Markdown
Author

Updated implementation example -

class UserRole(str, Enum):
    ADMIN = "admin"
    USER = "user"
    GUEST = "guest"

decorator = SQLTableSchemaDecorator(
    api_key=self.api_key,
    base_url=self.base_url,
    model=self.model,
    cache_dir=perf_cache_dir
)

@decorator
class User(BaseModel):
    """User model with auto-generated SQL schema."""
    id: str
    name: str
    email: str
    role: UserRole
    created_at: Optional[datetime] = None

TABLE_SCHEMA = User.get_sql_schema()

# The TABLE_SCHEMA will contain something like:
# CREATE TABLE IF NOT EXISTS users (
#     id VARCHAR(36) PRIMARY KEY NOT NULL,
#     name VARCHAR(255) NOT NULL,
#     email VARCHAR(255) NOT NULL,
#     role VARCHAR(50) NOT NULL CHECK (role IN ('admin', 'user', 'guest')),
#     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
# );

…ss itself instead of creating another function and adding decorator there. Updated it's test as well
@ojasaklechat41
Copy link
Copy Markdown
Author

ojasaklechat41 commented Jun 22, 2025

  • Implemented Jinja templating creating new templates folder and storing all the prompts as .j2 file.
  • Moved all the system prompt into prompts folder
  • Added overridding of the system prompts
  • Update the query creating prompt, now instead of using backticks ( `` ), it will be generating the queries with single quotes ( ' ' ) as backticks were giving errors while using those queries in multiple places.
  • Update the some test files as they were failing and giving error due to some prompt changes.

@ojasaklechat41
Copy link
Copy Markdown
Author

ojasaklechat41 commented Aug 14, 2025

@anshum4n-git

I have added a yet another implementation. Below is how we can do it

from typing import Optional
from pydantic import BaseModel
from foundation_sql.query import SQLQueryDecorator

class User(BaseModel):
    id: str
    name: str
    email: str
    created_at: Optional[str] = None

create_user = SQLQueryDecorator(
    db_url="sqlite:///./app.db",
    cache_dir="__sql__",
    auto_schema=True,          # enable automatic schema
    schema_validate=True,      # apply/validate schema on the DB
    schema_regen=False,        # do not force schema regeneration if cached
)

@create_user
def create_user(user: User) -> User:
    """Create a new user"""
    # body not used; SQL is LLM-generated
    ...

Now we can just add some new parameters in the decorator and then it will automatically track the schema and query based on that.

I have also updated the readme with the explanation of the edge cases

@anshum4n-git anshum4n-git changed the base branch from main to dev September 2, 2025 04:49
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.

Schema Generation Support

3 participants