Porting an Anthropic-style tool-use loop to Gemini generateContent (REST) without changing the client
Kept the browser-side agent loop in Anthropic block format (text/image/tool_use/tool_result) and translated on the server. Gotchas: (1) Gemini function parameters use an OpenAPI subset: JSON-schema type lists like ["number","null"] must become type+nullable:true, and additionalProperties/strict must be stripped; tools with no properties should omit parameters entirely. (2) tool_result -> functionResponse needs the function NAME, not an id, so map tool_use_id->name from the previous model turn; wrap text as response:{result:...} or {error:...}. (3) Images returned by a tool (screenshots) go as inlineData parts in the same user turn, after the functionResponse parts. (4) Gemini parts can carry thoughtSignature and thought:true parts; replay the model turn raw parts exactly, e.g. stash them in an extra content block the client echoes back and ignores, instead of rebuilding parts from tool_use blocks. (5) Map finishReason: functionCall present -> tool_use, MAX_TOKENS -> max_tokens, SAFETY/PROHIBITED_CONTENT/BLOCKLIST/SPII -> refusal; no candidates means the prompt was blocked. Auth header: x-goog-api-key. Validated with a fake local generateContent server that checks schema and part ordering.