fix(openai): omit absent tool description, never serialise a null schema - #8
Merged
Merged
Conversation
The OpenAI adapter built each tool with `json!`, so both optional fields
serialised as explicit nulls when unset:
"description": tool.description // None -> null
"parameters": tool.schema // None -> null
Neither is valid. `description` is optional in the OpenAI function schema, and
omitting a key is not the same as sending `null` — providers that validate the
payload reject a null where a string is expected. `parameters` is worse: it is
*required* to be a schema object, so `null` is not a legal value at all.
Builds the function object explicitly instead: `description` is inserted only
when present, and `parameters` falls back to `{"type": "object"}` — the empty
schema, which is what a tool taking no arguments actually means.
`strict: false` is deliberately unchanged. It is the documented default, and
altering it would change structured-output behaviour for every adapter that
shares this serialiser; the existing TODO about `strict: true` needing
`additionalProperties: false` is left in place for whoever takes that on.
Reachable in practice: terraphim-llm-proxy accepts tools in both the OpenAI
shape (`{type, function:{...}}`) and the Anthropic shape (`{name,
input_schema}`), and the Anthropic form frequently carries no description.
## Tests
Five in `adapter_shared::tests`:
- absent description is omitted rather than nulled
- present description still serialises
- absent schema falls back to the empty object schema, never null
- present schema passes through verbatim
- `strict` and the `type: function` envelope are preserved
The first and third fail against the previous code.
Note: `cargo clippy -- -D warnings` fails on `src/webc/web_stream.rs`
(collapsible_match). That is pre-existing on main and untouched here.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The OpenAI adapter built each tool with
json!, so both optional fields serialised as explicit nulls when unset:Neither is valid.
descriptionis optional in the OpenAI function schema, and omitting a key is not the same as sendingnull— providers that validate the payload reject a null where a string is expected.parametersis worse: it is required to be a schema object, sonullis not a legal value at all.Builds the function object explicitly instead:
descriptionis inserted only when present, andparametersfalls back to{"type": "object"}— the empty schema, which is what a tool taking no arguments actually means.strict: falseis deliberately unchanged. It is the documented default, and altering it would change structured-output behaviour for every adapter sharing this serialiser. The existing TODO aboutstrict: trueneedingadditionalProperties: falseis left for whoever takes that on.Why it is reachable
terraphim-llm-proxy accepts tools in both the OpenAI shape (
{type, function:{...}}) and the Anthropic shape ({name, input_schema}), and the Anthropic form frequently carries no description. It began routing tools through this serialiser in terraphim-llm-proxy#89.Tests
Five added; the first and third fail against the previous code.
strictand thetype: functionenvelope preservedcargo test --lib: 70 passed.cargo fmt --check: clean.Pre-existing, untouched:
cargo clippy -- -D warningsfails onsrc/webc/web_stream.rs(collapsible_match) on main as well.