BadRequestError error message

Hello everyone

I m a fresh starter following is my code which ends up with the following error message :slight_smile:

BadRequestError: Error code: 400 - {‘error’: {‘message’: ‘invalid model ID’, ‘type’: ‘invalid_request_error’, ‘param’: None, ‘code’: None}}

it seems something is wrong with last instruction : passing parameter i do not know how to fix it

please help

from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv(override=True)

client= OpenAI(api_key=os.environ.get("OPENAI_KEY"))

MODEL_NAME ="openai/gpt-4o-mini"

def get_completion(messages, \*\*kwargs):
if 'model' not in kwargs:
    kwargs\['model'\] = MODEL_NAME

response = client.chat.completions.create(
            messages=messages, \*\*kwargs
         )
return response

TEST_LIB_NAME =\[ {
"role": "user",
"content" : "Brainstorm a creative name for a new Python testing library."
}\]

print(get_completion(TEST_LIB_NAME,temperature=0.1).choices\[0\].message.content)

Welcome to the community!

your error message is pretty clear: error’: {‘message’: ‘invalid model ID’,

I would recommend going here:

and using the form to test and configure your configuration. Afterwards, you can click on [</> Code], (top right)

and select your environment

this will give you code you can directly copy into your project as a starting point.


PS: note the difference between your code and this code!

After getting the API right initially, everything becomes substantially easier going forward.

Good luck!

it generates a solution that does not solve the issue..After pasting the code i still get the same error

The model name does not have a “path” to it. I don’t know where you obtained "openai/gpt-4o-mini", but that is not a valid model ID to send to the API. That is the primary cause of your issue, reflected in the API error message.

Additionally, the Python OpenAI library will automatically get the environment variable seen; you do not need to explicitly place it as a client instantiation parameter.

If you completed the platform site’s “start building” wizard, it might suggest an initial model name that you can use, even for test calls before purchasing API credits. That might be gpt-5.4-mini or gpt-5.6-luna.

Using Responses API endpoint, by OpenAI recommendation and because developer functions are blocked on Chat Completions API endpoint with new models (if you decide to go more advanced), we can make less convoluted code and use the SDK’s helper method to get the output text:

from openai import OpenAI
# only if actually using .env file for OPENAI_API_KEY=
#from dotenv import load_dotenv
#load_dotenv()

MODEL_NAME = "gpt-5.6-luna"
client = OpenAI()

library_name_prompt = "Brainstorm a creative name for a new Python testing library."

api_response = client.responses.create(
    model=MODEL_NAME,
    input=library_name_prompt,
    reasoning={"effort": "low"},
    # temperature not tolerated by reasoning models
)

suggested_library_name = api_response.output_text
print(suggested_library_name)

I have to submit my code few times after few interractions finally it was solved…thanks for all your contributions Regards

Hi @armand_21, glad to hear the issue has been resolved.

The original 400 BadRequestError occurred because openai/gpt-4o-mini was not a valid model ID for the OpenAI API. The openai/ prefix needed to be removed, and the request needed to use a model available to the API project. The later example also showed how to use the current Responses API and clarified that the SDK can automatically read OPENAI_API_KEY from the environment.

Thank you, @Diet, for sharing the Playground-based troubleshooting steps, and @j for identifying the model-name issue and providing an updated code example. @armand_21, Thank you for confirming that the solution worked.