If you have an existing Vanna 0.x codebase and want to adopt the Vanna 2.0+ agent framework with minimal changes, use the LegacyVannaAdapter. This strategy allows you to keep your existing VannaBase instance (including database connections and training data) while gaining access to new features like the Web UI and streaming responses.
Key steps:
- Install Vanna 2.0+ with necessary extras:
pip install 'vanna[flask,anthropic]' (adjust extras based on your provider). - Implement a
UserResolver (required in 2.0+). - Wrap your existing
vn object with LegacyVannaAdapter(vn). - Initialize an
Agent using an llm_service and the adapter as the tool_registry. - Run the
VannaFastAPIServer.
What the adapter provides:
- Wraps
vn.run_sql() as the run_sql tool. - Exposes training data via
search_saved_correct_tool_uses. - Allows admins to save new training data via
save_question_tool_args.
from vanna import Agent, AgentConfig
from vanna.servers.fastapi import VannaFastAPIServer
from vanna.core.user import UserResolver, User, RequestContext
from vanna.legacy.adapter import LegacyVannaAdapter
from vanna.integrations.anthropic import AnthropicLlmService
# 1. Your existing 0.x object
# vn = MyVanna(config={'model': 'gpt-4', 'api_key': 'your-key'})
# vn.connect_to_postgres(...)
# 2. Define required UserResolver
class SimpleUserResolver(UserResolver):
async def resolve_user(self, request_context: RequestContext) -> User:
user_email = request_context.get_cookie('vanna_email')
if not user_email:
raise ValueError("Missing 'vanna_email' cookie")
return User(id=user_email, email=user_email, group_memberships=['user'])
# 3. Wrap existing vn with adapter
tools = LegacyVannaAdapter(vn)
# 4. Setup LLM and Agent
llm = AnthropicLlmService(model="claude-haiku-4-5", api_key="YOUR_KEY")
agent = Agent(
llm_service=llm,
tool_registry=tools,
user_resolver=SimpleUserResolver(),
config=AgentConfig()
)
# 5. Run server
server = VannaFastAPIServer(agent)
if __name__ == "__main__":
server.run(host='0.0.0.0', port=8000)