google-gemini / generative-ai-swift

The official Swift library for the Google Gemini API
https://ai.google.dev/gemini-api/docs/get-started/tutorial?lang=swift
Apache License 2.0
864 stars 131 forks source link

Log server error responses without `-GoogleGenerativeAIDebugLogEnabled` #177

Closed andrewheard closed 1 month ago

andrewheard commented 1 month ago

Updated logging to print response payloads even when -GoogleGenerativeAIDebugLogEnabled is not enabled. This is a port of https://github.com/firebase/firebase-ios-sdk/pull/13009.

Example Error Response Log If a `responseMIMEType` of `application/json` (JSON mode) is specified for a model that doesn't support it (e.g., `gemini-1.0-pro`): ``` [GoogleGenerativeAI] Response payload: { "error": { "code": 400, "message": "Json mode is not enabled for models/gemini-1.0-pro", "status": "INVALID_ARGUMENT" } } ```

Updated logging to only print the NSHTTPURLResponse if -GoogleGenerativeAIDebugLogEnabled is enabled.

Example NSHTTPURLResponse Log ``` [GoogleGenerativeAI] The server responded with an error: { URL: https://generativelanguage.googleapis.com/v1beta/models/gemini-1.0-pro:generateContent } { Status Code: 400, Headers { "Alt-Svc" = ( "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000" ); "Cache-Control" = ( private ); "Content-Encoding" = ( gzip ); "Content-Length" = ( 138 ); "Content-Type" = ( "application/json; charset=UTF-8" ); Date = ( "Wed, 29 May 2024 14:35:12 GMT" ); Server = ( "scaffolding on HTTPServer2" ); Vary = ( Origin, "X-Origin", Referer ); "server-timing" = ( "gfet4t7; dur=250" ); "x-content-type-options" = ( nosniff ); "x-frame-options" = ( SAMEORIGIN ); "x-xss-protection" = ( 0 ); } } ```

These were swapped because the NSHTTPURLResponse is rarely useful for understanding an error on its own, whereas the response payload contains details to fix the problem.