Skip to content

Conversation

eiixy
Copy link
Contributor

@eiixy eiixy commented Jul 7, 2025

When handling the response format for ChatCompletionResponseFormatJSONSchema, the field
response_format.json_schema.schema fails to unmarshal properly

Issue: #1027

Copy link

codecov bot commented Jul 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.97%. Comparing base (c125ae2) to head (6525e48).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1028      +/-   ##
==========================================
+ Coverage   85.83%   85.97%   +0.13%     
==========================================
  Files          43       43              
  Lines        2316     2338      +22     
==========================================
+ Hits         1988     2010      +22     
  Misses        305      305              
  Partials       23       23              

☔ 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.

@AyushSawant18588
Copy link
Contributor

@eiixy thanks for opening a PR for this. I tested these change but I am still seeing the following error :

json: cannot unmarshal object into Go struct field ChatCompletionResponseFormatJSONSchema.response_format.json_schema.schema of type openai.JSONSchema

I think issue stems from the Schema field in ChatCompletionResponseFormatJSONSchema being defined as an interface JSONSchema.
Go's JSON unmarshaler cannot instantiate interface types directly, it needs to know what concrete type to create. When it encounters the schema field, it doesn't know whether to create a jsonschema.Definition or another type that implements JSONSchema.

Are you also seeing this error or am I missing something?

@eiixy
Copy link
Contributor Author

eiixy commented Jul 8, 2025

@eiixy thanks for opening a PR for this. I tested these change but I am still seeing the following error :

json: cannot unmarshal object into Go struct field ChatCompletionResponseFormatJSONSchema.response_format.json_schema.schema of type openai.JSONSchema

I think issue stems from the Schema field in ChatCompletionResponseFormatJSONSchema being defined as an interface JSONSchema. Go's JSON unmarshaler cannot instantiate interface types directly, it needs to know what concrete type to create. When it encounters the schema field, it doesn't know whether to create a jsonschema.Definition or another type that implements JSONSchema.

Are you also seeing this error or am I missing something?

Hey, I just pushed some new tweaks to address the issue you mentioned. Could you pull the latest code and give it another go?

@AyushSawant18588
Copy link
Contributor

Its working now, changes lgtm. Thanks!

@sashabaranov sashabaranov requested a review from Copilot July 8, 2025 10:05
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR ensures that ChatCompletionResponseFormatJSONSchema.schema is properly unmarshalled into a jsonschema.Definition and adds tests to validate the behavior.

  • Introduces a custom UnmarshalJSON for ChatCompletionResponseFormatJSONSchema to handle object and null schema values correctly.
  • Adds unit tests for ChatCompletionResponseFormatJSONSchema.UnmarshalJSON and nested ChatCompletionRequest unmarshalling.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
chat.go Implements a custom UnmarshalJSON to correctly parse the schema field.
chat_test.go Adds tests for schema unmarshalling scenarios, including valid object, null, and invalid inputs.
Comments suppressed due to low confidence (2)

chat_test.go:960

  • [nitpick] Several test cases use empty strings for the name field, which can make it hard to identify failing subtests. Consider providing descriptive names for each scenario.
			"",

chat_test.go:954

  • The tests for ChatCompletionResponseFormatJSONSchema.UnmarshalJSON don't include a case where the description field is present in the input. Adding a test that includes a non-empty description will ensure that field is also unmarshalled correctly.
	tests := []struct {

Copy link
Owner

@sashabaranov sashabaranov left a comment

Choose a reason for hiding this comment

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

Thank you for the PR!

@sashabaranov sashabaranov merged commit 8e9b2ac into sashabaranov:master Jul 8, 2025
3 checks passed
Rosenberg96 pushed a commit to lunarway/go-openai that referenced this pull request Aug 18, 2025
…ONSchema.schema (sashabaranov#1028)

* feat: sashabaranov#1027

* add tests

* feat: sashabaranov#1027

* feat: sashabaranov#1027

* feat: sashabaranov#1027

* update chat_test.go

* feat: sashabaranov#1027

* add test cases
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.

3 participants