Skip to content

fix: normalize tool schemas so required ⊇ properties for OpenAI/Codex - #47

Merged
kevincodex1 merged 1 commit into
Twigpine:mainfrom
gnanam1990:fix/agent-tool-schema-openai
Apr 1, 2026
Merged

kevincodex1 merged 1 commit into
Twigpine:mainfrom
gnanam1990:fix/agent-tool-schema-openai

Conversation

@gnanam1990

Copy link
Copy Markdown
Collaborator

Problem

OpenAI and Codex enforce strict JSON Schema validation — every key in properties must also appear in required. Anthropic schemas often mark fields as optional (omitted from required), which causes 400 errors immediately on any OpenAI/Codex endpoint.

Reported in #46. Example error:

API Error: Codex API error 400: {
  "error": {
    "message": "Invalid schema for function 'Agent': In context=(), 'required' is required to be supplied and to be an array including every key in properties. Missing 'subagent_type'.",
    "type": "invalid_request_error",
    "param": "tools[0].parameters",
    "code": "invalid_function_parameters"
  }
}

The Agent tool has subagent_type in properties but not in required — valid for Anthropic, rejected by OpenAI.

Fix

Added normalizeSchemaForOpenAI() in convertTools() in openaiShim.ts. Before sending any tool schema to the API, it ensures required is a superset of all properties keys:

  • Existing required entries are preserved
  • Missing keys are appended
  • Schemas without properties (e.g. type: 'string') pass through unchanged

This fixes the issue for the Agent tool and any other tool with optional properties, without touching individual tool definitions.

Test

Tested locally with CLAUDE_CODE_USE_OPENAI=1 and GPT-4o:

spawn an agent to list files in current directory

Before fix: immediate 400 error on tool schema validation
After fix: Agent(List files in current directory) → Done (1 tool use · 8.9k tokens · 8s)

Closes #46.

OpenAI and Codex enforce strict JSON Schema validation — every key in
`properties` must also appear in `required`. Anthropic schemas often
mark fields as optional (omitted from `required`), which causes 400
errors on OpenAI/Codex endpoints.

Example: the Agent tool has `subagent_type` in `properties` but not
in `required`, producing:
  "Invalid schema for function 'Agent': Missing 'subagent_type'
   in required array"

Fix: add `normalizeSchemaForOpenAI()` in `convertTools()` that ensures
`required` is a superset of all `properties` keys before the schema is
sent to the API. Existing `required` entries are preserved; missing
ones are appended. Schemas without `properties` pass through unchanged.

Fixes Twigpine#46.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@kevincodex1
kevincodex1 merged commit 5f774cf into Twigpine:main Apr 1, 2026
euxaristia pushed a commit to euxaristia/openclaude that referenced this pull request Apr 13, 2026
…openai

fix: normalize tool schemas so required ⊇ properties for OpenAI/Codex
reymaster pushed a commit to reymaster/openclaude that referenced this pull request May 5, 2026
…openai

fix: normalize tool schemas so required ⊇ properties for OpenAI/Codex
thedeveloloper pushed a commit to thedeveloloper/openclaude that referenced this pull request Jun 8, 2026
…openai

fix: normalize tool schemas so required ⊇ properties for OpenAI/Codex
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.

Agent tool schema incompatible with OpenAI/Codex API — missing subagent_type in required array

2 participants