Safety_settings not working when using OpenAI compatibility extra body

from openai import OpenAI

client = OpenAI(
    api_key=MY_API_KEY,
    base_url="https://generativelanguage.googleapis.com/v1beta/"
)

stream = client.chat.completions.create(
    model="gemini-2.5-pro-preview-03-25",
    n=1,
    messages=[
        {
            "role": "user",
            "content": "Summarize the video"
        }
    ],
    stream=True,
    stream_options={'include_usage': True},
    extra_body={
        'extra_body':
        {
            'google': {
              'cached_content': "cachedContents/0000aaaa1111bbbb2222cccc3333dddd4444eeee"
          }
        }
    }
)

for chunk in stream:
    print(chunk)
    print(chunk.usage.to_dict())

This is what the documentation at OpenAI compatibility  |  Gemini API  |  Google AI for Developers says. I tried replacing it with safety settings like:

“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_MEDIUM_AND_ABOVE”},
{“category”: “HARM_CATEGORY_DANGEROUS_CONTENT”, “threshold”: “BLOCK_NONE”},
{“category”: “HARM_CATEGORY_CIVIC_INTEGRITY”, “threshold”: “BLOCK_NONE”}
]
}
}

And it does not work. Either the documentation is wrong or the backend implementation is not active yet.

1 Like

Hi @aliencaoao , Welcome to the forum.

I have notified the engineering team about the issue. I will follow up with you once I have more information.

Thanks!

Just a heads up: the safety setting feature is not implemented yet in OAI Compatibility. We will update the doc to reflect that. Sorry about the documentation issue.

Is there a timeline for this? I would also like to be able to manage safety settings through the compatibility layer.

1 Like

And when will this functionality be implemented and available for OAI Compatibility? This is a very important setting and it is required for operation in many cases!

Hi it seems the docs is still yet to be updated.

Looks like docs are updated, but we don’t have the feature to pass safety_settings yet :frowning: