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

Support for Structured Outputs #814

Closed
hari4698 opened this issue Aug 7, 2024 · 7 comments
Closed

Support for Structured Outputs #814

hari4698 opened this issue Aug 7, 2024 · 7 comments
Labels
enhancement New feature or request

Comments

@hari4698
Copy link

hari4698 commented Aug 7, 2024

I was wondering if there will support in the future for Structured Outputs. Open AI released this feature on August 6th.

Python example from the Open API docs:

from pydantic import BaseModel
from openai import OpenAI

client = OpenAI()

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

completion = client.beta.chat.completions.parse(
    model="gpt-4o-2024-08-06",
    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,
)

event = completion.choices[0].message.parsed
@hari4698 hari4698 added the enhancement New feature or request label Aug 7, 2024
@h0rv
Copy link

h0rv commented Aug 7, 2024

I am looking to add support for the strict json schema too.

instructor-go currently gives a similar experience as the python API provides, and in place for Pydantic it use Go struct tags and jsonschema. Adding the API support will improve the performance of the model generating the correct format.

@modeledge
Copy link

Great news.

I'm not sure how you will approach implementing the actual json schema definition within the request but consider allowing for different implementations of the actual json schema creation. I started with field tags, as in instructor-go, but have since moved away from that approach and think field tags are too limiting.

@h0rv
Copy link

h0rv commented Aug 10, 2024

For instructor, JSON tags has been sufficient.

I have been struggling to convert the json schema to the currently defined struct here.

I think it would be great to optionally pass a raw schema to response_format.

For example, the current struct does not support defs, which are crucial, especially with complex data types. The OpenAI API supports this: https://platform.openai.com/docs/guides/structured-outputs/supported-schemas.

@monstercameron
Copy link

This enhancement would be great!

@rjcorwin
Copy link

Looks like this issue is resolved? #813

@h0rv
Copy link

h0rv commented Aug 28, 2024

My issue above is resolved: for those wanting to use their own schema string, and not use the go-openai/jsonschema, you can do the following:

JSONSchema: &openai.ChatCompletionResponseFormatJSONSchema{
    Name:        "my-name",
    Schema:      json.RawMessage(schemaJSONString),
    Strict:      true,
}

@hari4698
Copy link
Author

Support added for Structured Output. #813

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants