Skip to content

openai.BadRequestError: Error code: 400 - {'error': {'message': "'$.messages[0].content' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", 'type': 'invalid_request_error', 'param': None, 'code': None}} #1270

@pradeepdev-1995

Description

@pradeepdev-1995

Confirm this is an issue with the Python library and not an underlying OpenAI API

  • This is an issue with the Python library

Describe the bug

Tried the below AsyncAzureOpenAI

import asyncio
import openai
import time
openai.api_type = "azure"
openai.api_base = "<base url>"
openai.api_version = "<version>"
openai.api_key = "<api key>"
instructions = [prompt1,prompt2,prompt3]
from tqdm import tqdm
async_client = openai.AsyncAzureOpenAI(
    azure_endpoint="<base url>",
    api_key="<api key>",
    api_version="<version>"
    )

async def aync_main(prompt: str) -> None:
    response = await async_client.chat.completions.create(
        model="<model>",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content
async def async_run_loop(instructions):
    tasks = [aync_main(prompt) for prompt in enumerate(tqdm(instructions))]
    answers = await asyncio.gather(*tasks)
    return {idx: answer for idx, answer in enumerate(answers)}

async_answers = asyncio.run(async_run_loop(instructions))
print(async_answers)

But it returns the given error

openai.BadRequestError: Error code: 400 - {'error': {'message': "'$.messages[0].content' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", 'type': 'invalid_request_error', 'param': None, 'code': None}}

To Reproduce

Tried the below AsyncAzureOpenAI

import asyncio
import openai
import time
openai.api_type = "azure"
openai.api_base = "<base url>"
openai.api_version = "<version>"
openai.api_key = "<api key>"
instructions = [prompt1,prompt2,prompt3]
from tqdm import tqdm
async_client = openai.AsyncAzureOpenAI(
    azure_endpoint="<base url>",
    api_key="<api key>",
    api_version="<version>"
    )

async def aync_main(prompt: str) -> None:
    response = await async_client.chat.completions.create(
        model="<model>",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content
async def async_run_loop(instructions):
    tasks = [aync_main(prompt) for prompt in enumerate(tqdm(instructions))]
    answers = await asyncio.gather(*tasks)
    return {idx: answer for idx, answer in enumerate(answers)}

async_answers = asyncio.run(async_run_loop(instructions))
print(async_answers)

But it returns the given error

openai.BadRequestError: Error code: 400 - {'error': {'message': "'$.messages[0].content' is invalid. Please check the API reference: https://platform.openai.com/docs/api-reference.", 'type': 'invalid_request_error', 'param': None, 'code': None}}

Code snippets

No response

OS

Ubuntu

Python version

Python 3.9.18

Library version

1.13.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions