How to specify SafetySettings via OpenAI Python SDK

As per: OpenAI compatibility  |  Gemini API  |  Google AI for Developers
It can be specified a safety_settings field. However, all of my, and all of those provided by Gemini 2.5 Pro (w/ Search Grounding & w/ Code Execution) were rejected by the Google API endpoint:

https://generativelanguage.googleapis.com/v1beta/openai/

My attempt (strictly following the nesting at thinking_budget or caching examples in the official docs):

extra_body = {
                "extra_body": {
                    "google": {
                        "safety_settings": [
                            {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_NONE"},
                            {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE"},
                            {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_NONE"},
                            {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_NONE"},
                        ]
                    }
                }
            }

response = await client.chat.completions.create(
                model=langmodel,
                messages=[
                    {"role": "system", "content": instructions},
                    *history,
                ],
                extra_body=extra_body
            )

API Response:

[{'error': {'code': 400, 'message': 'Invalid JSON payload received. Unknown name "safety_settings" at \'extra_body.google\': Cannot find field.', 'status': 'INVALID_ARGUMENT', 'details': [{'@type': 'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations': [{'field': 'extra_body.google', 'description': 'Invalid JSON payload received. Unknown name "safety_settings" at \'extra_body.google\': Cannot find field.'}]}]}}]

After several other variants, Gemini 2.5 Pro concluded:

This is an incredibly frustrating situation, and I sincerely apologize that my previous suggestions, based on standard practices and even your provided documentation link, have not worked. Thank you for your persistence and for providing the exact error messages each time.

The sequence of errors leads to a very strong and, unfortunately, disappointing conclusion. Let’s reason through this:

  1. You are using the correct SDK parameter: extra_body is the designated way to add non-standard, top-level parameters to a request using the OpenAI SDK.
  2. You have tried every logical structure:
    • Passing the parameters directly ("safetySettings": ...).
    • Passing them in a generation_config block.
    • Passing them in a "google": {} block, as per the official Google documentation.
  3. The API has rejected every single attempt: The error message has consistently been Unknown name "...", where “…” is the top-level key you tried to add (safetySettings, generation_config, and even google).

This means the https://generativelanguage.googleapis.com/v1beta/openai/ endpoint, in its current state, does not recognize any custom top-level parameters. It appears to be enforcing a strict schema that only includes the standard OpenAI fields (model, messages, etc.) and nothing else.

Conclusion: There is a discrepancy between Google’s documentation and the actual behavior of their v1beta OpenAI-compatible API endpoint. The feature to pass safetySettings through this compatibility layer does not appear to be implemented or enabled, despite being documented.

You are not doing anything wrong. Your last attempt, with extra_body = {"google": {"safetySettings": [...]}}, is exactly what the documentation specifies and should have worked. The API is not honoring its own documentation.

The Solution: Use the Native Google AI SDK

When a provider’s OpenAI compatibility layer is incomplete or buggy, the only reliable solution is to switch to their native SDK, which will always have first-class support for all its features.

Here is how you can accomplish your goal directly using Google’s own google-generativeai Python library:

First, install the library:
pip install google-generativeai

Then, use the following code to make your request. It’s quite similar to the OpenAI SDK but allows for direct, reliable access to all of Gemini’s features.

:gemini:Gemini 2.5 Pro Chat Record (on my issue)

Last note: I am NOT sending any abusive nor violent prompts to the API endpoints. Yet I’m getting “PROHIBITED_CONTENT” on almost every request from gemini-2.5-* models. All prior models (like gemini-2.0-flash) work flawlessly and 99.99 % of my requests are passed through the default content filtering. Since introduction of Gemini 2.5 lineup, the models just do not work when using OpenAI SDK. Hence I am in need to adjust the SafetySettings, as they likely became outrageously strict in comparison to all older Gemini models.