Skip to content

Commit 7455476

Browse files
authored
Merge pull request Wei-Shaw#918 from rickylin047/fix/responses-string-input
fix(openai): convert string input to array for Codex OAuth responses endpoint (fix Wei-Shaw#919)
2 parents 3cc407b + 9f1f203 commit 7455476

2 files changed

Lines changed: 60 additions & 0 deletions

File tree

backend/internal/service/openai_codex_transform.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ func applyCodexOAuthTransform(reqBody map[string]any, isCodexCLI bool, isCompact
146146
input = filterCodexInput(input, needsToolContinuation)
147147
reqBody["input"] = input
148148
result.Modified = true
149+
} else if inputStr, ok := reqBody["input"].(string); ok {
150+
// ChatGPT codex endpoint requires input to be a list, not a string.
151+
// Convert string input to the expected message array format.
152+
trimmed := strings.TrimSpace(inputStr)
153+
if trimmed != "" {
154+
reqBody["input"] = []any{
155+
map[string]any{
156+
"type": "message",
157+
"role": "user",
158+
"content": inputStr,
159+
},
160+
}
161+
} else {
162+
reqBody["input"] = []any{}
163+
}
164+
result.Modified = true
149165
}
150166

151167
return result

backend/internal/service/openai_codex_transform_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,50 @@ func TestApplyCodexOAuthTransform_NonCodexCLI_PreservesExistingInstructions(t *t
249249
require.Equal(t, "old instructions", instructions)
250250
}
251251

252+
func TestApplyCodexOAuthTransform_StringInputConvertedToArray(t *testing.T) {
253+
reqBody := map[string]any{"model": "gpt-5.4", "input": "Hello, world!"}
254+
result := applyCodexOAuthTransform(reqBody, false, false)
255+
require.True(t, result.Modified)
256+
input, ok := reqBody["input"].([]any)
257+
require.True(t, ok)
258+
require.Len(t, input, 1)
259+
msg, ok := input[0].(map[string]any)
260+
require.True(t, ok)
261+
require.Equal(t, "message", msg["type"])
262+
require.Equal(t, "user", msg["role"])
263+
require.Equal(t, "Hello, world!", msg["content"])
264+
}
265+
266+
func TestApplyCodexOAuthTransform_EmptyStringInputBecomesEmptyArray(t *testing.T) {
267+
reqBody := map[string]any{"model": "gpt-5.4", "input": ""}
268+
result := applyCodexOAuthTransform(reqBody, false, false)
269+
require.True(t, result.Modified)
270+
input, ok := reqBody["input"].([]any)
271+
require.True(t, ok)
272+
require.Len(t, input, 0)
273+
}
274+
275+
func TestApplyCodexOAuthTransform_WhitespaceStringInputBecomesEmptyArray(t *testing.T) {
276+
reqBody := map[string]any{"model": "gpt-5.4", "input": " "}
277+
result := applyCodexOAuthTransform(reqBody, false, false)
278+
require.True(t, result.Modified)
279+
input, ok := reqBody["input"].([]any)
280+
require.True(t, ok)
281+
require.Len(t, input, 0)
282+
}
283+
284+
func TestApplyCodexOAuthTransform_StringInputWithToolsField(t *testing.T) {
285+
reqBody := map[string]any{
286+
"model": "gpt-5.4",
287+
"input": "Run the tests",
288+
"tools": []any{map[string]any{"type": "function", "name": "bash"}},
289+
}
290+
applyCodexOAuthTransform(reqBody, false, false)
291+
input, ok := reqBody["input"].([]any)
292+
require.True(t, ok)
293+
require.Len(t, input, 1)
294+
}
295+
252296
func TestIsInstructionsEmpty(t *testing.T) {
253297
tests := []struct {
254298
name string

0 commit comments

Comments
 (0)