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

GenAI Client safety settings does not support multiple SafetySetting #654

Closed
victai opened this issue Dec 20, 2024 · 4 comments
Closed

GenAI Client safety settings does not support multiple SafetySetting #654

victai opened this issue Dec 20, 2024 · 4 comments
Assignees
Labels
component:python sdk Issue/PR related to Python SDK status:awaiting user response Awaiting a response from the author type:help Support-related issues

Comments

@victai
Copy link

victai commented Dec 20, 2024

Description of the bug:

VertexAI Client works fine, fine, but the new GenAI Client (google.genai.Client) does not support multiple SafetySetting.

SafetySettings applied

safety_settings = [
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
]

Error message:

400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'Multiple safety settings with the same category.', 'status': 'INVALID_ARGUMENT'}}

The error message is incorrect and misleading, which makes me think this is a bug.

If I only apply one safety setting it'll work, but whenever there are multiple, it claims that it is setting the same category.

Actual vs expected behavior:

Actual behavior: If I only apply one safety setting it'll work, but whenever there are multiple, it claims that it is setting the same category.
Expected behavior: should work the same as the vertexai client.

Any other information you'd like to share?

Both synchronous and asynchronous clients/functions have the issue mentioned above. Here's an asynchronous script that demonstrates the issue

import asyncio
from google import genai
from google.genai.types import GenerateContentConfig
from vertexai.generative_models import HarmBlockThreshold, HarmCategory, SafetySetting, GenerativeModel

safety_settings = [
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
        threshold=HarmBlockThreshold.BLOCK_NONE,
    ),
]

async def main():
    query = "How does AI work?"
    model = "gemini-1.5-flash-002"
    vertex_client = GenerativeModel(model)
    try:
        response = await vertex_client.generate_content_async(
            contents=query,
            safety_settings=safety_settings[:1],
        )
        print("vertex client single safety settings response: ", response.text[:50])
    except Exception as e:
        print("vertex client single safety settings error: ", e)

    try:
        response = await vertex_client.generate_content_async(
            contents=query,
            safety_settings=safety_settings,
        )
        print("vertex client multiple safety settings response: ", response.text[:50])
    except Exception as e:
        print("vertex client multiple safety settings error: ", e)

    genai_client = genai.Client(vertexai=True, location="us-east4")
    try:
        response = await genai_client.aio.models.generate_content(
            model=model, contents=query, config=GenerateContentConfig(safety_settings=safety_settings[:1])
        )
        print("genai client single safety settings response: ", response.text[:50])
    except Exception as e:
        print("genai client single safety settings error: ", e)

    try:
        response = await genai_client.aio.models.generate_content(
            model=model, contents=query, config=GenerateContentConfig(safety_settings=safety_settings)
        )
        print("genai client multiple safety settings response: ", response.text[:50])
    except Exception as e:
        print("genai client multiple safety settings error: ", e)

asyncio.run(main())
@Gunand3043 Gunand3043 self-assigned this Dec 23, 2024
@Gunand3043 Gunand3043 added type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team component:python sdk Issue/PR related to Python SDK type:help Support-related issues and removed type:bug Something isn't working labels Dec 23, 2024
@Gunand3043
Copy link

Hi @victai

The new Google GenAI SDK expects the safety category values to be strings. You need to pass the safety settings in the format below.

safety_settings = [
     types.SafetySetting(
         category="HARM_CATEGORY_DANGEROUS_CONTENT",
         threshold="BLOCK_NONE",
     ),
     types.SafetySetting(
         category="HARM_CATEGORY_HATE_SPEECH",
         threshold="BLOCK_NONE",
     ),
     types.SafetySetting(
         category="HARM_CATEGORY_HARASSMENT",
         threshold="BLOCK_NONE",
     ),
      types.SafetySetting(
         category="HARM_CATEGORY_SEXUALLY_EXPLICIT",
         threshold="BLOCK_NONE",
     ),
     types.SafetySetting(
         category="HARM_CATEGORY_CIVIC_INTEGRITY",
         threshold="BLOCK_NONE",
     ),
 ]

Thanks

@Gunand3043 Gunand3043 added status:awaiting user response Awaiting a response from the author and removed status:triaged Issue/PR triaged to the corresponding sub-team labels Dec 23, 2024
@victai
Copy link
Author

victai commented Dec 26, 2024

@Gunand3043 that doesn't work for me. The result is the same. I'm using google-generativeai==0.8.3, which is the latest version.

@Gunand3043 Gunand3043 removed the status:awaiting user response Awaiting a response from the author label Dec 27, 2024
@Gunand3043
Copy link

Oh, sorry to hear that you're still facing the issue. Since you're using google-generativeai, we have a quickstart tutorial on customizing multiple safety settings. Here’s the notebook link: notebook.

If you're using the new Google Gen AI SDK, I’ve also prepared a colab gist that shows how to pass multiple safety settings.

Hope this helps!

@Gunand3043 Gunand3043 added the status:awaiting user response Awaiting a response from the author label Dec 27, 2024
@victai
Copy link
Author

victai commented Jan 3, 2025

Thanks for the response. The issue was that I was using SafetySettings from vertexai.generative_models not google.genai.types

@victai victai closed this as completed Jan 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:python sdk Issue/PR related to Python SDK status:awaiting user response Awaiting a response from the author type:help Support-related issues
Projects
None yet
Development

No branches or pull requests

2 participants