openai / openai-python

The official Python library for the OpenAI API
https://pypi.org/project/openai/
Apache License 2.0
22.75k stars 3.18k forks source link

with multiprocessing, pickle issue with client.beta.chat.completions.parse output #1776

Closed yufang67 closed 1 week ago

yufang67 commented 3 weeks ago

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

Describe the bug

We use multiprocessing to handle the calls. It was working with client.chat.completions.create. Currently, we are trying client.beta.chat.completions.parse with response_format, i got error:

Traceback (most recent call last): File "/anaconda/lib/python3.10/multiprocessing/queues.py", line 244, in _feed obj = _ForkingPickler.dumps(obj) File "/anaconda/lib/python3.10/multiprocessing/reduction.py", line 51, in dumps cls(buf, protocol).dump(obj) _pickle.PicklingError: Can't pickle <class 'openai.types.chat.parsed_chat_completion.ParsedChatCompletion[CalendarEvent]'>: attribute lookup ParsedChatCompletion[CalendarEvent] on openai.types.chat.parsed_chat_completion failed

anyone can help ?

Thanks

To Reproduce

from pydantic import BaseModel
from . import get_token_provider
import openai
import asyncio
import pickle

class CalendarEvent(BaseModel):
    name: str
    date: str
    participants: list[str]

def original_test():

    MODEL = "gpt-4o-2024-08-06"
    ENDPOINT = "gpt-4o-2024-08-06-endpoint"
    token_provider = get_token_provider()

    client = openai.AsyncAzureOpenAI(
    azure_ad_token_provider=token_provider,
    azure_endpoint=ENDPOINT,
    api_version="2024-08-01-preview",
    )

    func = client.beta.chat.completions.parse(
        model=MODEL,
        messages=[
            {"role": "system", "content": "Extract the event information."},
            {"role": "user", "content": "Alice and Bob are going to a science fair on Friday."},
        ],
        response_format=CalendarEvent,
        )

    completion = asyncio.run(func)
    pickle.dumps(completion)

if __name__ == "__main__":
    original_test()

Code snippets

No response

OS

linux

Python version

py3.10

Library version

openai 1.51.0

yufang67 commented 1 week ago

Based on Function calling with structured outputs in this page https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/structured-outputs?tabs=python-secure I use client.chat.completions.create instead of client.beta.chat.completions.parse Its resolved.