Skip to content

v2 ChatCompletionMessage.ToParams() returns an empty slice of ToolCalls #309

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

Closed
wangxj03 opened this issue Mar 22, 2025 · 7 comments
Closed

Comments

@wangxj03
Copy link

wangxj03 commented Mar 22, 2025

When no tool call is present, ToParams returns an empty slice. This causes an issue when continuing the conversation, as the chat completion API expects tool_calls to contain at least one element.

panic: POST "https://api.openai.com/v1/chat/completions": 400 Bad Request {
            "message": "Invalid 'messages[4].tool_calls': empty array. Expected an array with minimum length 1, but got an empty array instead.",
            "type": "invalid_request_error",
            "param": "messages[4].tool_calls",
            "code": "empty_array"
          }

Trace:

Image
@wangxj03
Copy link
Author

@jacobzim-stl please take a look at this v2 issue. The code was working fine in v1. Thanks!

@adrianliechti
Copy link

adrianliechti commented Mar 22, 2025

I noted that this method returns nulls Content too.
In the screenshot, there is Content set, but the JSON.Content not. Which results that the toParam Method returns an empty Value

if meta.IsPresent() {
	return param.NewOpt(value)
}

if meta.IsExplicitNull() {
	return param.NullOpt[T]()
}

return param.Opt[T]{}
Image

@jacobzim-stl
Copy link
Collaborator

Thanks for the report, working on shipping a fix now.

@adrianliechti
Copy link

adrianliechti commented Mar 22, 2025

@jacobzim-stl

just saw your commit:

if r.JSON.Content.IsPresent() {
	p.Content.OfString.Value = r.Content
}
if r.JSON.Refusal.IsPresent() {
	p.Refusal = String(r.Refusal)
}

just for your info:
p.Content is still not set after the method; even if non-empty on r.Content

Maybe you have to set something like:

if r.Content != "" {
p.Content.OfString.Value = r.Content
}

before the JSON Blocks; or ensure the JSON Values are actual set (they are all empty)

r.JSON.Content => Empty
r.Content => set

I am using a a openai.ChatCompletionAccumulator{}

@jacobzim-stl
Copy link
Collaborator

Thanks for the catch

@adrianliechti
Copy link

adrianliechti commented Mar 22, 2025

ciao @jacobzim-stl,

thanks a ton for your fast hacking!
I can confirm it look much better now!

maybe another hint for the relase notes:
openai.ToolMessage seems to change the parameters (toolID, content vs. content, toolID)
since both were strings, this was a bit tricky to figure out :)

@jacobzim-stl
Copy link
Collaborator

The ToParam issue should be resolved.

@adrianliechti I appreciate the feedback, I'll add a note to the migration guide.

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

No branches or pull requests

3 participants