-
Notifications
You must be signed in to change notification settings - Fork 55
Fix incorrect formatting in OpenAI responses API for tool #125
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1050,59 +1050,59 @@ private enum Responses { | |||||||||||||||||
| outputs.append(object) | ||||||||||||||||||
|
|
||||||||||||||||||
| case .tool(let id): | ||||||||||||||||||
| let toolMessage = msg | ||||||||||||||||||
| // Wrap user content into a single top-level message as required by Responses API | ||||||||||||||||||
| var contentBlocks: [JSONValue] | ||||||||||||||||||
| switch toolMessage.content { | ||||||||||||||||||
| let outputValue: JSONValue | ||||||||||||||||||
| switch msg.content { | ||||||||||||||||||
| case .text(let text): | ||||||||||||||||||
| contentBlocks = [ | ||||||||||||||||||
| .object(["type": .string("input_text"), "text": .string(text)]) | ||||||||||||||||||
| ] | ||||||||||||||||||
| outputValue = .string(text) | ||||||||||||||||||
| case .blocks(let blocks): | ||||||||||||||||||
| contentBlocks = blocks.map { block in | ||||||||||||||||||
| switch block { | ||||||||||||||||||
| case .text(let text): | ||||||||||||||||||
| return .object(["type": .string("input_text"), "text": .string(text)]) | ||||||||||||||||||
| case .imageURL(let url): | ||||||||||||||||||
| return .object([ | ||||||||||||||||||
| "type": .string("input_image"), | ||||||||||||||||||
| "image_url": .object(["url": .string(url)]), | ||||||||||||||||||
| ]) | ||||||||||||||||||
| outputValue = .array( | ||||||||||||||||||
| blocks.map { block in | ||||||||||||||||||
| switch block { | ||||||||||||||||||
| case .text(let text): | ||||||||||||||||||
| return .object(["type": .string("input_text"), "text": .string(text)]) | ||||||||||||||||||
| case .imageURL(let url): | ||||||||||||||||||
| return .object([ | ||||||||||||||||||
| "type": .string("input_image"), | ||||||||||||||||||
| "image_url": .string(url), | ||||||||||||||||||
| ]) | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
| let outputString: String | ||||||||||||||||||
| if contentBlocks.count > 1 { | ||||||||||||||||||
| let encoder = JSONEncoder() | ||||||||||||||||||
| if let data = try? encoder.encode(JSONValue.array(contentBlocks)), | ||||||||||||||||||
| let str = String(data: data, encoding: .utf8) | ||||||||||||||||||
| { | ||||||||||||||||||
| outputString = str | ||||||||||||||||||
| } else { | ||||||||||||||||||
| outputString = "[]" | ||||||||||||||||||
| } | ||||||||||||||||||
| } else if let block = contentBlocks.first { | ||||||||||||||||||
| let encoder = JSONEncoder() | ||||||||||||||||||
| if let data = try? encoder.encode(block), | ||||||||||||||||||
| let str = String(data: data, encoding: .utf8) | ||||||||||||||||||
| { | ||||||||||||||||||
| outputString = str | ||||||||||||||||||
| } else { | ||||||||||||||||||
| outputString = "{}" | ||||||||||||||||||
| } | ||||||||||||||||||
| } else { | ||||||||||||||||||
| outputString = "{}" | ||||||||||||||||||
| ) | ||||||||||||||||||
| } | ||||||||||||||||||
| outputs.append( | ||||||||||||||||||
| .object([ | ||||||||||||||||||
| "type": .string("function_call_output"), | ||||||||||||||||||
| "call_id": .string(id), | ||||||||||||||||||
| "output": .string(outputString), | ||||||||||||||||||
| "output": outputValue, | ||||||||||||||||||
| ]) | ||||||||||||||||||
| ) | ||||||||||||||||||
|
|
||||||||||||||||||
| case .raw(rawContent: let rawContent): | ||||||||||||||||||
| outputs.append(rawContent) | ||||||||||||||||||
| // Convert Chat Completions assistant+tool_calls to Responses API function_call items | ||||||||||||||||||
| if case .object(let dict) = rawContent, | ||||||||||||||||||
| case .string(let role) = dict["role"], | ||||||||||||||||||
| role == "assistant", | ||||||||||||||||||
| case .array(let toolCallsArr) = dict["tool_calls"] | ||||||||||||||||||
| { | ||||||||||||||||||
| for tc in toolCallsArr { | ||||||||||||||||||
| guard case .object(let tcDict) = tc, | ||||||||||||||||||
| case .string(let tcId) = tcDict["id"], | ||||||||||||||||||
| case .object(let fnDict) = tcDict["function"], | ||||||||||||||||||
| case .string(let fnName) = fnDict["name"], | ||||||||||||||||||
| case .string(let fnArgs) = fnDict["arguments"] | ||||||||||||||||||
|
Comment on lines
+1089
to
+1092
|
||||||||||||||||||
| case .string(let tcId) = tcDict["id"], | |
| case .object(let fnDict) = tcDict["function"], | |
| case .string(let fnName) = fnDict["name"], | |
| case .string(let fnArgs) = fnDict["arguments"] | |
| case .string(let tcId)? = tcDict["id"], | |
| case .object(let fnDict)? = tcDict["function"], | |
| case .string(let fnName)? = fnDict["name"], | |
| case .string(let fnArgs)? = fnDict["arguments"] |
Copilot
AI
Feb 12, 2026
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.
This PR changes request-body formatting for Responses tool-call round trips, but there’s no deterministic unit test asserting the generated input items (e.g., .raw assistant+tool_calls → function_call items, and .tool → function_call_output with a plain string output). Adding a unit test around Responses.createRequestBody would prevent regressions without requiring a live API key.
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.
The pattern matches against
dict["role"]/dict["tool_calls"]are missing optional unwrapping (?). Sincedict[...]is optional (as used elsewhere in this file, e.g.case let .string(type)? = obj["type"]), this won’t compile. Update these tocase .string(let role)? = .../case .array(let toolCallsArr)? = ...(or unwrap withguard let).