Skip to content

Sarvam AI: add tool calling support#145

Open
alfredjimmy-redbus wants to merge 3 commits intoredbus-labs:mainfrom
alfredjimmy-redbus:feature/sarvam-tool-implementation
Open

Sarvam AI: add tool calling support#145
alfredjimmy-redbus wants to merge 3 commits intoredbus-labs:mainfrom
alfredjimmy-redbus:feature/sarvam-tool-implementation

Conversation

@alfredjimmy-redbus
Copy link

Tool Calling Implementation – Sarvam AI Contrib

Overview

Tool calling allows Sarvam AI to work with the ADK’s function tools. The model receives tool definitions and can return tool calls; the runner executes tools and sends results back.


Files Edited

1. ChatMessage.java

Role: Support tool calls and tool responses in messages.

Field Type Use case
tool_calls List<ChatToolCall> Assistant messages with function calls
tool_call_id String Tool messages linked to a specific call

Add getters/setters for both fields.


2. ChatToolCall.java

Role: Map Sarvam’s tool call format (OpenAI-style).

Structure: id, type="function", function: { name, arguments } (JSON string).


3. ChatRequest.java

Role: Convert ADK LlmRequest → Sarvam chat request, including tools and prior tool use.

Edit Description
tools field List of tools in Sarvam format
tool_choice field Set to "auto" when tools are present
fromLlmRequest Map Part.functionCall → assistant message with tool_calls; Part.functionResponse → tool message with tool_call_id
buildTools() Convert FunctionDeclaration{ type, function: { name, description, parameters } }
schemaToMap() Convert Schema to Map for JSON
schemaTypeToString() Map Type enum → string ("string", "number", etc.)
getTools(), getToolChoice() Getters for JSON serialization
Imports Add Schema, Type from com.google.genai.types

4. SarvamAi.java

Role: Convert Sarvam responses → ADK LlmResponse.

Edit Description
toLlmResponse() Support both text and tool-call responses
Tool-call branch If effectiveMsg.getToolCalls() is non-empty → parse each ChatToolCall → create FunctionCall.builder().id().name().args() → build Part.functionCall → set Content parts → return LlmResponse
Text branch If no tool calls → use Part.fromText(content) as before
Imports Add ChatToolCall, FunctionCall, ImmutableList, TypeReference, ArrayList, List, Map

Data Flow

  1. Request: LlmRequest with tools and history → ChatRequest.fromLlmRequest() → request JSON.
  2. Response (text): Sarvam message.contenttoLlmResponse()Part.fromText()LlmResponse.
  3. Response (tool call): Sarvam message.tool_callstoLlmResponse()Part.functionCall()LlmResponse for the ADK runner.

@alfredjimmy-redbus alfredjimmy-redbus changed the title Feature/sarvam tool implementation Sarvam AI: add tool calling support Mar 18, 2026
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.

1 participant