Skip to content

Check Gemini response success before trying to parse usage metadata #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 13, 2025

Conversation

andyw8
Copy link
Contributor

@andyw8 andyw8 commented Jun 13, 2025

If the API key is missing, Gemini will respond with:

{
  "error": {
    "code": 403,
    "message": "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
    "status": "PERMISSION_DENIED"
  }
}

so response["usageMetadata"]["promptTokenCount"] will raise an exception about a method being called on nil.

(there may be use cases where there is an error but usageMetadata is still available. If so, then a check for that key's presence would be better).

@andyw8 andyw8 force-pushed the andyw8/check-response-success branch from 804819c to 9b23db7 Compare June 13, 2025 19:08
@andyw8
Copy link
Contributor Author

andyw8 commented Jun 13, 2025

(similar issues may be present for other providers).

@swerner
Copy link
Contributor

swerner commented Jun 13, 2025

Thanks @andyw8!

@swerner
Copy link
Contributor

swerner commented Jun 13, 2025

(similar issues may be present for other providers).

Yeah probably...I had been putting this change off as I was thinking about a better way to handle logging...potentially even creating a Provider base class...but guess I'd put it off too long...

@swerner swerner merged commit a236b96 into sublayerapp:main Jun 13, 2025
1 check passed
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.

2 participants