Custom Tools are an Enterprise feature. Talk to us to enable them for your account.
Build specialized tools with the conv-ai-sdk for advanced logic like database queries, complex calculations, or stateful operations.
Uploading custom tools file

OnStart - Initialize conversations

Executed at the beginning of a conversation, typically for initialization. The class must be named OnStart.
import httpx
from pydantic import Field

from sarvam_conv_ai_sdk import (
    SarvamOnStartTool,
    SarvamOnStartToolContext,
    SarvamToolLanguageName,
)

class OnStart(SarvamOnStartTool):
    async def run(self, context: SarvamOnStartToolContext):
        user_id = context.get_user_identifier()
        async with httpx.AsyncClient() as client:
            response = await client.get(f"https://sarvam-flights.com/users/{user_id}")
            response.raise_for_status()
            user_data = response.json()

        source_destination = user_data.get("home_city")
        context.set_agent_variable("source_destination", source_destination)
        context.set_agent_variable("passenger_name", user_data.get("name"))
        
        if context.provider_ref_id:
            context.set_agent_variable("call_sid", context.provider_ref_id)
        
        context.set_initial_language_name(SarvamToolLanguageName.ENGLISH)
        context.set_initial_bot_message(
            f"Hello! Would you like to book a flight from {source_destination}? Where would you like to go?",
        )
        return context

Context Methods

Variable Management

  • get_agent_variable(variable_name: str) -> Any — Retrieve a variable
  • set_agent_variable(variable_name: str, value: Any) -> None — Set a variable

Initialization

  • set_initial_bot_message(message: str) -> None — Set the first message
  • set_initial_state_name(state_name: str) -> None — Set initial state
  • set_initial_language_name(language: SarvamToolLanguageName) -> None — Set initial language

User & Metadata

  • get_user_identifier() -> str — Get user ID
  • provider_ref_id: Optional[str] — Reference ID from channel (Call SID for telephony)
  • get_engagement_metadata() -> EngagementMetadata — Get engagement info

Best Practices

  1. Always implement run() — The entry point for tool execution logic
  2. Use Field() for parameters — Ensures type safety and LLM prompt metadata
  3. Gracefully handle errors — Avoid accessing unset variables or invalid types
  4. Return appropriate typesSarvamTool.run() returns SarvamToolOutput; OnStart and OnEnd return their context objects
  5. Write meaningful docstrings — Clearly describe tool purpose for LLM tool-calling
  6. Use async operations — Use async/await for I/O to avoid blocking
  7. Use context methods — Prefer provided context methods over direct attribute access