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

core: Add additional properties recursively for structured output(openai) when nested object is generated #25169

Merged

Conversation

fufufukakaka
Copy link
Contributor

@fufufukakaka fufufukakaka commented Aug 8, 2024

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

{
  "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!

@efriis efriis added the partner label Aug 8, 2024
@efriis efriis self-assigned this Aug 8, 2024
@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 8, 2024
Copy link

vercel bot commented Aug 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Aug 23, 2024 0:05am

@dosubot dosubot bot added Ɑ: core Related to langchain-core 🔌: openai Primarily related to OpenAI integrations 🤖:improvement Medium size change to existing code to handle new use-cases labels Aug 8, 2024
@fufufukakaka fufufukakaka changed the title core: Add additional properties recursively for structured output(openai) core: Add additional properties recursively for structured output when nested object is generated(openai) Aug 8, 2024
@fufufukakaka fufufukakaka changed the title core: Add additional properties recursively for structured output when nested object is generated(openai) core: Add additional properties recursively for structured output(openai) when nested object is generated Aug 8, 2024
) -> None:
"""Test to verify structured output with strict=True for nested object."""

from typing import TypedDict
Copy link
Contributor Author

@fufufukakaka fufufukakaka Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now, pydantic outputs json schema includes 'allOf', but openai don't support.
pydantic PR → pydantic/pydantic#10029

BadRequestError: Error code: 400 - {'error': {'message': "Invalid schema for response_format 'JokeWithEvaluation': In context=('properties', 'self_evaluation'), 'allOf' is not permitted", 'type': 'invalid_request_error', 'param': 'response_format', 'code': None}}

full error: https://gist.github.com/fufufukakaka/0f638b40687a00fc712819f7b3dd0b5e

So, I'll only implement tests using TypedDict. 🙏

@ccurme ccurme requested a review from baskaryan August 8, 2024 13:14
@eyurtsev eyurtsev assigned baskaryan and unassigned efriis Aug 14, 2024
@sajal2692
Copy link
Contributor

When is this expected to be merged? would be super useful

@efriis efriis self-assigned this Aug 21, 2024
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add unit tests for this functionality instead of integration tests?

@baskaryan baskaryan added the needs test PR needs to be updated with tests label Aug 22, 2024
@dosubot dosubot bot removed the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 23, 2024
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Aug 23, 2024
@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label Aug 23, 2024
@baskaryan baskaryan enabled auto-merge (squash) August 23, 2024 00:06
@baskaryan baskaryan merged commit 0258cb9 into langchain-ai:master Aug 23, 2024
112 checks passed
@baskaryan
Copy link
Collaborator

thank you @fufufukakaka! will be out in langchain-core 0.2.35, which will be released later today

@fufufukakaka
Copy link
Contributor Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: core Related to langchain-core 🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. needs test PR needs to be updated with tests 🔌: openai Primarily related to OpenAI integrations partner size:L This PR changes 100-499 lines, ignoring generated files.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

4 participants