Skip to content
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

What does models.generateContent return when the request is *not successful*? #317

Closed
matejsarlija opened this issue Feb 12, 2025 · 5 comments
Assignees
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.

Comments

@matejsarlija
Copy link

matejsarlija commented Feb 12, 2025

Thank you for making SDK.

First of all, when using the AI studio the code in "Get code" does not mesh with the suggested genai SDK.

I have an existing API in place, but can't figure out from your docs what happens when the request fails, or is denied due to non-AI reasons (tokens limit, safety etc.). So how do I parse the usual HTTP status codes from your response?

In the code it's mentioned ~ Response body
If successful, the response body contains an instance of GenerateContentResponse.

Could you also add what happens when the request is not succesful, as it's really hard to find?

Thanks!

@matejsarlija matejsarlija added priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue. labels Feb 12, 2025
@sararob sararob self-assigned this Feb 12, 2025
@sararob
Copy link
Contributor

sararob commented Feb 12, 2025

First of all, when using the AI studio the code in "Get code" does not mesh with the suggested genai SDK.

Could you give a specific example?

So how do I parse the usual HTTP status codes from your response?

You can use this error class. For example (this returns a 404):

try:
  client.models.generate_content(
      model="invalid-model-name",
      contents="What is your name?",
  )
except errors.APIError as e:
  print(e)

Could you also add what happens when the request is not succesful

Yes good suggestion, we'll update our docs.

@matejsarlija
Copy link
Author

matejsarlija commented Feb 13, 2025

Thank you for the errors, that helped. In regard to the "get code" from Google AI Studio, you get something in the lines of the following -

import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GEMINI_API_KEY"])

# Create the model
generation_config = {
  "temperature": 1,
  "top_p": 0.95,
  "top_k": 64,
  "max_output_tokens": 8192,
  "response_mime_type": "text/plain",
}

model = genai.GenerativeModel(
  model_name="gemini-2.0-pro-exp-02-05",
  generation_config=generation_config,
)

chat_session = model.start_chat(
  history=[
  ]
)

response = chat_session.send_message("INSERT_INPUT_HERE")

print(response.text)

The google.generativeai repo listed corresponds to the following https://github.com/GoogleCloudPlatform/generative-ai, and not the one we're currently in, right?

copybara-service bot pushed a commit that referenced this issue Feb 13, 2025
@sararob
Copy link
Contributor

sararob commented Feb 13, 2025

We'll get the AI Studio code snippet updated, thanks for your bug report!

copybara-service bot pushed a commit that referenced this issue Feb 13, 2025
PiperOrigin-RevId: 726463667
copybara-service bot pushed a commit that referenced this issue Feb 13, 2025
PiperOrigin-RevId: 726475680
@jamg7 jamg7 assigned jamg7 and unassigned sararob Feb 13, 2025
@matejsarlija
Copy link
Author

Appreciate it. Should I close this now, or how does it work?

@jamg7
Copy link
Collaborator

jamg7 commented Feb 14, 2025

Thanks, @matejsarlija

Closing this ticket for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants