Skip to content

Support json schema for response format type in OpenAIChatCompletionClient#5988

Merged
ekzhu merged 7 commits intomainfrom
ekzhu-openai-response-format
Mar 18, 2025
Merged

Support json schema for response format type in OpenAIChatCompletionClient#5988
ekzhu merged 7 commits intomainfrom
ekzhu-openai-response-format

Conversation

@ekzhu
Copy link
Contributor

@ekzhu ekzhu commented Mar 18, 2025

Resolves #5982

This PR adds support for json_schema as a response_format type in OpenAIChatCompletionClient. This is necessary because it allows the client to be serialized along with the schema. If user use response_format=SomeBaseModel, the client cannot be serialized.

Usage:

# Structured output response, with a pre-defined JSON schema.

OpenAIChatCompletionClient(...,
response_format = {
    "type": "json_schema",
    "json_schema": {
        "name": "name of the schema, must be an identifier.",
        "description": "description for the model.",
        # You can convert a Pydantic (v2) model to JSON schema
        # using the `model_json_schema()` method.
        "schema": "<the JSON schema itself>",
        # Whether to enable strict schema adherence when
        # generating the output. If set to true, the model will
        # always follow the exact schema defined in the
        # `schema` field. Only a subset of JSON Schema is
        # supported when `strict` is `true`.
        # To learn more, read
        # https://platform.openai.com/docs/guides/structured-outputs.
        "strict": False,  # or True
    },
},
)

@ekzhu ekzhu requested review from lspinheiro and victordibia March 18, 2025 00:50
Copy link
Contributor

@jorge-wonolo jorge-wonolo left a comment

Choose a reason for hiding this comment

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

Works perfect to solve the issue filed under #5982

Copy link
Collaborator

@lspinheiro lspinheiro left a comment

Choose a reason for hiding this comment

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

I think there are still some issues with the tests, but the main change looks good.

@codecov
Copy link

codecov bot commented Mar 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 76.70%. Comparing base (09d8d34) to head (9616b5c).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5988      +/-   ##
==========================================
+ Coverage   76.68%   76.70%   +0.02%     
==========================================
  Files         191      191              
  Lines       13152    13164      +12     
==========================================
+ Hits        10085    10097      +12     
  Misses       3067     3067              
Flag Coverage Δ
unittests 76.70% <100.00%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ekzhu ekzhu enabled auto-merge (squash) March 18, 2025 03:10
@ekzhu ekzhu merged commit a8cef32 into main Mar 18, 2025
53 checks passed
@ekzhu ekzhu deleted the ekzhu-openai-response-format branch March 18, 2025 03:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to dump_component for OpenAIChatCompletionClient using a pydantic as response_format

3 participants