Skip to content

JSON is not supported by (Azure) OpenAI API #637

@younes-io

Description

@younes-io

Describe the issue as clearly as possible:

I need to generate valid JSON as the output of an email classifier, and I need to be 100% sure that the output will always be valid since it'll be the input of an API call.
So, I took the JSON example from the docs and tried it using OpenAI, but it failed.

Steps/code to reproduce the bug:

from pydantic import BaseModel

from outlines import models, generate


class User(BaseModel):
    name: str
    last_name: str
    id: int

model = models.openai("gpt-3.5-turbo")
generator = generate.json(model, User)
result = generator("Create a user profile with the fields name, last_name and id")
print(result)

Expected result:

a JSON representing the User object

# User(name="John", last_name="Doe", id=11)

Error message:

NotImplementedError                       Traceback (most recent call last)
Cell In[13], line 14
     10     id: int
     13 model = models.openai("gpt-3.5-turbo")
---> 14 generator = generate.json(model, User)
     15 result = generator("Create a user profile with the fields name, last_name and id")
     16 print(result)

File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.12_3.12.752.0_x64__qbz5n2kfra8p0\Lib\functools.py:909, in singledispatch.<locals>.wrapper(*args, **kw)
    905 if not args:
    906     raise TypeError(f'{funcname} requires at least '
    907                     '1 positional argument')
--> 909 return dispatch(args[0].__class__)(*args, **kw)

File ~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\outlines\generate\json.py:76, in json_openai(model, schema_object, sampler)
     72 @json.register(OpenAI)
     73 def json_openai(
     74     model, schema_object: Union[str, object, Callable], sampler: Sampler = multinomial()
     75 ):
---> 76     raise NotImplementedError(
     77         "Cannot use JSON Schema-structure generation with an OpenAI model "
     78         + "due to the limitations of the OpenAI API"
     79     )


NotImplementedError: Cannot use JSON Schema-structure generation with an OpenAI model due to the limitations of the OpenAI API

Outlines/Python version information:

Version information

Details ``` $ python -c "from outlines import _version; print(_version.version)" 0.0.28 $ python -c "import sys; print('Python', sys.version)" Python 3.12.2 (tags/v3.12.2:6abddd9, Feb 6 2024, 21:26:36) [MSC v.1937 64 bit (AMD64)] ```

Context for the issue:

I'm building an email classifier for a client, so I need to have a consistent JSON output of the result of classification (a JSON listing the categories and their respective scores); unfortunately, this is not supported due to OpenAI limitations :/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions