Tool calling lets Gemini request your PHP functions by name during a conversation. Instead of returning a final answer, the model returns a structured request describing which function to call and with what arguments. The SDK parsesDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/google/llms.txt
Use this file to discover all available pages before exploring further.
function_call parts from the API response and wraps them as ToolCallPart objects, making it straightforward to detect, execute, and respond to them.
How it works
The function calling flow follows a multi-turn pattern:- Send the initial request with one or more tool definitions attached.
- Gemini responds with a
ToolCallPart—$result->finishReasonwill beFinishReason::ToolCallswhenever afunction_callpart is detected in the response. - Your code executes the function locally using the
nameandargumentsfrom theToolCallPart. - Send a follow-up request containing a
tool_resultmessage with the output of that execution. - Gemini produces the final text response, incorporating the tool result.
Defining tools
Pass an array of tool definitions to->tools([...]) on your generate call. Each tool describes a function name, a description, and the parameters it accepts as a JSON schema.
Handling the tool call response
After running the request, check whether Gemini is requesting a function call before processing$result->text.
Multi-turn loop: sending the tool result back
After executing the function, send the result back to Gemini as atool_result message. Include the original assistant turn so the model has full context.
Streaming tool calls
When streaming, the parser emits two parts for each tool call in sequence:ToolCallStartPart— carries theindex,id, andnameof the tool call.ToolCallDeltaPart— carries theindexand a JSON string of the (potentially partial) arguments.
args object rather than token-by-token JSON, there is typically a single ToolCallDeltaPart per call containing the full argument payload. Reconstruct the call by matching on index.
The finish reason is unconditionally set to
FinishReason::ToolCalls whenever a function_call part is detected. In non-streaming responses, GoogleResponseParser::finishReason() scans the parsed parts list and returns FinishReason::ToolCalls if any ToolCallPart is present, regardless of the finish_reason string sent by the API. In streaming responses, GoogleStreamParser::yieldDelta() overrides the running finish reason to FinishReason::ToolCalls as soon as it encounters a function_call delta.