Skip to content

Gemini Parser

Parser for Google Gemini function call responses.

gemini

Parser for Google Gemini function call responses.

GeminiResponseParser

Parses Gemini GenerateContentResponse into AgentResponse.

Handles the Gemini response format where tool calls appear as function_call parts in response.candidates[].content.parts[].

Works with both the raw dict format and the google-genai SDK objects.

Usage

parser = GeminiResponseParser() response = parser.parse(gemini_raw_response)

parse

parse(raw_response: Any) -> AgentResponse

Parse a Gemini response into a normalized AgentResponse.

Source code in src/russo/parsers/gemini.py
def parse(self, raw_response: Any) -> AgentResponse:
    """Parse a Gemini response into a normalized AgentResponse."""
    tool_calls: list[ToolCall] = []

    # Handle google-genai SDK response objects
    candidates = _get_attr_or_key(raw_response, "candidates", [])
    for candidate in candidates:
        content = _get_attr_or_key(candidate, "content", None)
        if content is None:
            continue
        parts = _get_attr_or_key(content, "parts", [])
        for part in parts:
            fc = _get_attr_or_key(part, "function_call", None)
            if fc is not None:
                name = _get_attr_or_key(fc, "name", "")
                args = _get_attr_or_key(fc, "args", {})
                if isinstance(args, str):
                    import json

                    args = json.loads(args)
                tool_calls.append(ToolCall(name=name, arguments=dict(args) if args else {}))

    return AgentResponse(tool_calls=tool_calls, raw=raw_response)