fix: handle undefined type in toGeminiSchema for enum/const-only schemas#370
fix: handle undefined type in toGeminiSchema for enum/const-only schemas#370nuthalapativarun wants to merge 6 commits into
Conversation
|
Hey @nuthalapativarun, my apology first: I went back and tested across versions, and #367 has actually been fixed since 0.2.5 (the |
|
Thanks @wanseob — no worries at all! You're right that the
Happy to scope it down to just the enum/const inference if the type-signature change is considered unnecessary noise. Let me know! |
| const schema = toGeminiSchema(input as unknown as MCPToolSchema); | ||
|
|
||
| expect(schema).toEqual({ | ||
| type: Type.NUMBER, |
There was a problem hiding this comment.
shouldn't it be instead?
expect(schema).toEqual({
type: Type.NUMBER,
enum: [42],
});
|
Good catch @kalenkevich! I've pushed the fix — One note on the value type: the existing |
|
Thanks for the update, please fix the tests |
The const-only schema branch was directly mutating mcp.type on the original input object before spreading it into a new mcp. On a second call with the same input, the type was already set so if (!mcp.type) was skipped, leaving enum unset. Fix by collecting the inferred type into a local variable and setting both type and enum atomically via the spread assignment. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Found and fixed the root cause: the const branch was directly mutating mcp.type on the original input object before spreading it into a new mcp. When the test calls oGeminiSchema twice with the same input object (once inside .not.toThrow() and once to capture the value), the type field is already set on the second call so if (!mcp.type) is skipped entirely — leaving enum unset. Fix: collect the inferred type into a local variable and set both ype and enum atomically via the spread assignment (mcp = {...mcp, type: inferredType, enum: [mcp.const]}). All 27 schema util tests + 1482 core unit tests now pass locally. |
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
type(MCP tool sub-schema withouttypefield) #367toGeminiType()incore/src/utils/gemini_schema_util.tshad its parameter typed asstring(notstring | undefined), andrecursiveConvertdid not handle schemas that define shape viaenumorconstwithout atypefield. This caused aTypeErrorat runtime when such schemas were encountered (e.g., from MCP tools).Changes:
toGeminiType()signature to acceptstring | undefined(the existing!mcpTypeguard already handles the undefined value safely).recursiveConvertto infer the primitive type fromenumvalues (when all enum entries share the same JS type) or from aconstvalue.enumfield to the resulting GeminiSchemaobject.Testing Plan
Unit Tests:
Manual E2E Tests:
typefield no longer crashChecklist