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

api-version query parameter is automatically added, causing bug when using OpenAITarget with non-OpenAI providers #762

Open
LeoVrana opened this issue Mar 6, 2025 · 3 comments

Comments

@LeoVrana
Copy link

LeoVrana commented Mar 6, 2025

Describe the bug

It looks like the OpenAITarget and its variants automatically include an api_version attribute, which defaults to an Azure OpenAI-specific value. This default is appended as a query parameter in API requests, causing failures when using the Google Generative Language API (OpenAI compatibility docs for that here).

Steps/Code to Reproduce

import os
from dotenv import load_dotenv
load_dotenv()

GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", None)
assert GOOGLE_API_KEY is not None

from pyrit.common.initialization import initialize_pyrit
from pyrit.memory.central_memory import CentralMemory
from pyrit.prompt_target.openai.openai_chat_target import OpenAIChatTarget
from pyrit.orchestrator import PromptSendingOrchestrator

initialize_pyrit(memory_db_type="InMemory")
memory = CentralMemory.get_memory_instance()

gemini_target = OpenAIChatTarget(model_name="gemini-1.5-flash", endpoint="https://generativelanguage.googleapis.com/v1beta/openai/", api_key=GOOGLE_API_KEY)
orchestrator = PromptSendingOrchestrator(objective_target=gemini_target)
                                         
response = await orchestrator.send_prompts_async(prompt_list=["Please say hi."])
await orchestrator.print_conversations_async()

Expected Results

It should be calling the endpoint as provided.

Actual Results

It is adding an api_version query parameter:

Part of the traceback:

HTTPStatusError: Client error '404 Not Found' for url 'https://generativelanguage.googleapis.com/v1beta/openai?api-version=2024-06-01'

Setting api_version=None does not prevent it from being added. Instead, it results in an empty query parameter:

HTTPStatusError: Client error '404 Not Found' for url 'https://generativelanguage.googleapis.com/v1beta/openai?api-version='

Versions

  • OS: WSL Ubuntu-22.04
  • Python version: 3.10.12
  • PyRIT version: [e.g. 0.1.0 or installed from main branch in editable mode]
  • version of Python packages:
System:
    python: 3.10.12 (main, Feb  4 2025, 14:57:36) [GCC 11.4.0]
executable: /home/leo/rt_env/bin/python
   machine: Linux-5.15.167.4-microsoft-standard-WSL2-x86_64-with-glibc2.35

Python dependencies:
        pyrit: 0.6.1.dev0
       Cython: None
        numpy: 2.2.3
       openai: 1.65.0
          pip: 22.0.2
 scikit-learn: 1.6.1
        scipy: 1.15.2
   setuptools: 59.6.0
   tensorflow: None
        torch: None
 transformers: 4.49.0  

I see that this parameter is added for the various OpenAI targets:

Proposed solution

I'd be very happy to submit a fix for this. A possible approach:

  • Modify handling of api_version so that setting it to None prevents it from being added to query parameters. Otherwise, the default is maintained to avoid breaking existing usage.
    Example:
params = {"api-version": self._api_version} if self._api_version is not None else {}
  • Or, open to other suggestions if you have a different preferred approach.
@romanlutz
Copy link
Contributor

When you say Google Generative Language API, is that Gemini or something else? I'm not sure aware of their offerings but we've requested an account to be able to add integration tests. I should have that shortly. Do you think you could add one that uses Gemini with a Gemini specific api key? Just to make sure requests go through.

@LeoVrana
Copy link
Author

LeoVrana commented Mar 7, 2025

Yes, I am referring to Gemini. That endpoint is what they provide for use with the OpenAI SDK. However, I have found that I need to add "/chat/completions" to that endpoint in order to make it work with Gemini as an OpenAIChatTarget (when using it with the fix in #764).

I would be very happy to add an integration test to confirm Gemini support, I just want to make sure I follow your preferred setup. Should that test go in test_targets_and_secrets.py, or do you prefer a new file?

@romanlutz
Copy link
Contributor

Sorry, I'm a little behind myself! I wasn't aware that this part of the integration tests existed already. I am actually getting an account today and can add the integration test with that. I would ignore it for your PR. I don't want to add the test before I have the account + key. That said, if I add the secret to our integration test pipeline today I'll confirm here so that you can add the corresponding parametrization to the integration test.

Thanks for confirming that it's Gemini and thanks your PR!

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

No branches or pull requests

2 participants