-
Notifications
You must be signed in to change notification settings - Fork 15.4k
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
openai[patch]: ChatOpenAI.with_structured_output json_schema support #25123
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -298,6 +302,8 @@ class _AllReturnType(TypedDict): | |||
class BaseChatOpenAI(BaseChatModel): | |||
client: Any = Field(default=None, exclude=True) #: :meta private: | |||
async_client: Any = Field(default=None, exclude=True) #: :meta private: | |||
root_client: Any = Field(default=None, exclude=True) #: :meta private: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
curious why is this necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to access the beta apis
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. Is the change to root poetry.lock related?
"schema must be specified when method is not 'json_mode'. " | ||
"Received None." | ||
) | ||
strict = strict if strict is not None else True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to confirm: does json_schema
with strict=False
work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yep
In previous versions, I wanted to force the llm's response to be formatted as JSON. For that, I used the bind method to define the response format. Here is the error:
Here is the code example I am using:
|
hm which version of the |
checking the openai version I get:
I assumed the sdk version is obtained the same way as libraries versions. Feel free to correct me if there is another way. |
I got the same error |
Hi @baskaryan, thanks for adding the support of JSON Schema to OpenAI, is planned to add it also to the AzureChat class ? |
the above azure openai error and general azure support should be included in langchain-openai==0.1.22, would love to know if that resolves the issues / works for you @Dahimi @PaLoic1 @ricnunespt |
Thanks @baskaryan for your feedback, I check the code of |
you're right, fixing here #25591 |
…strict (#25169) Hello. First of all, thank you for maintaining such a great project. ## Description In #25123, support for structured_output is added. However, `"additionalProperties": false` needs to be included at all levels when a nested object is generated. error from current code: https://gist.github.com/fufufukakaka/e9b475300e6934853d119428e390f204 ``` BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'JokeWithEvaluation': In context=('properties', 'self_evaluation'), 'additionalProperties' is required to be supplied and to be false", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}} ``` Reference: [Introducing Structured Outputs in the API](https://openai.com/index/introducing-structured-outputs-in-the-api/) ```json { "model": "gpt-4o-2024-08-06", "messages": [ { "role": "system", "content": "You are a helpful math tutor." }, { "role": "user", "content": "solve 8x + 31 = 2" } ], "response_format": { "type": "json_schema", "json_schema": { "name": "math_response", "strict": true, "schema": { "type": "object", "properties": { "steps": { "type": "array", "items": { "type": "object", "properties": { "explanation": { "type": "string" }, "output": { "type": "string" } }, "required": ["explanation", "output"], "additionalProperties": false } }, "final_answer": { "type": "string" } }, "required": ["steps", "final_answer"], "additionalProperties": false } } } } ``` In the current code, `"additionalProperties": false` is only added at the last level. This PR introduces the `_add_additional_properties_key` function, which recursively adds `"additionalProperties": false` to the entire JSON schema for the request. Twitter handle: `@fukkaa1225` Thank you! --------- Co-authored-by: Bagatur <[email protected]>
No description provided.