Skip to content

feat(py): Add support for dynamically defined tools#5214

Open
huangjeff5 wants to merge 22 commits intomainfrom
jh-dynamic-tools
Open

feat(py): Add support for dynamically defined tools#5214
huangjeff5 wants to merge 22 commits intomainfrom
jh-dynamic-tools

Conversation

@huangjeff5
Copy link
Copy Markdown
Contributor

@huangjeff5 huangjeff5 commented Apr 30, 2026

  • Add a tool() constructor.
  • Creates a per-call child registry to hold any inline tools that aren't stored in the Registry.

Sample usage:

import asyncio
from genkit import Genkit, tool
from genkit.plugins.google_genai import GoogleAI

DB = {
    'u-001': {'plan': 'Pro',  'balance': 1240,  'txns': ['2026-05-01 Invoice #88 -$500', '2026-04-28 Refund ORD-441 +$120']},
    'u-002': {'plan': 'Free', 'balance': 42.5,  'txns': ['2026-05-03 Top-up +$50']},
}

ai = Genkit(plugins=[GoogleAI()])

async def chat(user_id: str, msg: str) -> str:
    u = DB[user_id]
    async def account() -> str:
        """This user's account: plan and balance."""
        return f'plan={u["plan"]} balance=${u["balance"]}'
    async def transactions() -> list[str]:
        """Recent transactions for this user."""
        return u['txns']
    return (await ai.generate(
        model='googleai/gemini-2.0-flash', prompt=msg,
        tools=[tool(account, name='account'), tool(transactions, name='transactions')],
    )).text

async def main():
    for uid in ('u-001', 'u-002'):
        print(f'--- {uid} ---')
        print(await chat(uid, 'Summarize my account and transactions.'))

asyncio.run(main())

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for inline tools by scoping unregistered tool instances within temporary child registries during generation calls. It refactors the prompt execution pipeline to separate template rendering from generation options and adds a "resume" field to PromptConfig for enhanced tool response handling. Feedback was provided regarding a PEP 8 line length violation in _generate.py, suggesting a refactor for better readability.

Comment thread py/packages/genkit/src/genkit/_ai/_generate.py Outdated
Base automatically changed from jh-dap-pr1 to main April 30, 2026 16:45
An error occurred while trying to automatically change base from jh-dap-pr1 to main April 30, 2026 16:45
Comment thread py/packages/genkit/src/genkit/_ai/_prompt.py
@huangjeff5 huangjeff5 requested a review from pavelgj May 6, 2026 14:52
@huangjeff5 huangjeff5 marked this pull request as ready for review May 6, 2026 15:02
@huangjeff5 huangjeff5 requested a review from a team May 7, 2026 03:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant