Tutorials
Why tRPC OpenAPI Generated Incorrect Types for Optional Responses
A tRPC procedure can return undefined, while its generated OpenAPI schema previously promised that result.data would always exist. Version 11.32.2 fixes this mismatch.
A TypeScript client says that an API response contains a data property. The compiler accepts the code, autocomplete offers the field, and nothing appears wrong.
Then the request succeeds, but the property is missing.
Consider this response:
{
"result": {}
}
There is no data field, although the generated client may describe it as required.
This was a real issue in @trpc/openapi, fixed in tRPC 11.32.2, released on November 27, 2026.
The problem was not that tRPC returned invalid JSON. A procedure was allowed to return undefined, but the generated OpenAPI specification did not correctly represent that possibility.
The result was a mismatch between what the server could send and what generated TypeScript clients were told to expect.
Why undefined disappears from the response
JavaScript distinguishes between an object property that exists with the value undefined and a property that does not exist at all.
JSON does not preserve that distinction during ordinary object serialization.
Consider:
const response = {
result: {
data: undefined,
},
};
console.log(JSON.stringify(response));
// {"result":{}}
The result object remains, but data disappears.
Compare that with null:
console.log(
JSON.stringify({
result: {
data: null,
},
}),
);
// {"result":{"data":null}}
These values may both represent an absence of useful data at the application level, but they produce different JSON.
This becomes significant when an API schema promises that a particular field will always be present.
An OpenAPI generator must consider whether the server can omit a property, not simply whether its TypeScript type includes a particular value.
Reproduce the issue with a tRPC procedure
Create a router with a procedure whose output validator accepts undefined.
// src/router.ts
import { initTRPC } from "@trpc/server";
import { z } from "zod";
const t = initTRPC.create();
export const appRouter = t.router({
findMessage: t.procedure
.output(z.string().optional())
.query(() => undefined),
});
export type AppRouter = typeof appRouter;
The output validator is:
z.string().optional()
That means the result can be either a string or undefined.
The resolver returns undefined, so the standard JSON response envelope can omit data:
{
"result": {}
}
Before the fix, the OpenAPI generator could still describe the successful response as requiring data.
That was an incorrect contract.
The same problem affected .nullish() outputs when they returned undefined, as well as output types inferred without an explicit validator when undefined was allowed.
What was wrong in the generated OpenAPI schema?
OpenAPI describes which properties are required through the required array.
Before the fix, the relevant response schema could resemble:
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
},
"required": ["data"]
}
The required entry says that every object matching this schema must contain data.
But our procedure can return undefined, and JSON serialization removes the field.
The actual response therefore contradicts the schema.
After the fix, the corresponding schema becomes:
{
"type": "object",
"properties": {
"data": {
"type": "string"
}
}
}
The property is still documented, but it is no longer required.
These are simplified excerpts of the nested result schema, not complete OpenAPI documents.
The enclosing result property remains required. The change concerns the optional presence of result.data.
This distinction also affects generated TypeScript.
Previously:
type ResponseBefore = {
result: {
data: string;
};
};
After the correction:
type ResponseAfter = {
result: {
data?: string;
};
};
The second type accurately describes a response where data may be absent.
What changed inside tRPC 11.32.2?
The fix was introduced in PR #8220.
Previously, the generator decided whether data was required mainly by checking whether an output schema existed.
That was insufficient because a defined schema can still accept undefined.
The updated implementation tracks an additional property:
outputOptional: acceptsUndefined(outputType)
When building the response envelope, the generator now adds data to the required properties only when the output does not permit undefined.
The relevant condition changed to:
hasOutput && !outputOptional
This also preserves the correct behavior for procedures whose outputs are always present.
For example:
requiredMessage: t.procedure
.output(z.string())
.query(() => "Hello"),
Here, data remains required.
And there is a difference between nullable and nullish:
| Zod output | Can return undefined? | Is data required? |
|---|---|---|
z.string() |
No | Yes |
z.string().nullable() |
No | Yes |
z.string().optional() |
Yes | No |
z.string().nullish() |
Yes | No |
A nullable result may contain null, but the property itself still exists.
A nullish result may contain either null or undefined, so the field cannot be guaranteed.
Update the OpenAPI generator
The fix is available in:
@trpc/[email protected]
Notice the -alpha suffix.
The OpenAPI generator is published separately from the stable tRPC packages.
For a compatible project, install the corrected release:
pnpm add @trpc/[email protected]
The package’s published peer requirements at this version include:
@trpc/[email protected]- TypeScript
>=7.1.0-beta - Zod
>=4.0.0, when using Zod
Check these requirements before upgrading.
In particular, projects still using TypeScript 5.x or 6.x should not assume that replacing the OpenAPI package alone will work.
The OpenAPI integration remains in alpha, and the version requirements may be significant for an existing application.
Regenerate the OpenAPI specification
Once your dependencies are compatible, regenerate the specification from the router.
The official tRPC generator provides a CLI:
pnpm exec trpc-openapi ./src/router.ts \
-e AppRouter \
-o openapi.json \
--title "Example API" \
--version 1.0.0
The multiline command above uses Unix-style line continuations. On Windows PowerShell, enter it on one line or use PowerShell’s continuation syntax.
The -e AppRouter option identifies the exported router type from the earlier example.
After generation, inspect the successful response for GET /findMessage.
Look for the schema describing result.
The data property should no longer appear in that object’s required array.
If you generate TypeScript clients from the OpenAPI specification, regenerate those as well.
For example, with Hey API:
pnpm exec openapi-ts -i openapi.json -o ./generated
The precise output depends on your Hey API configuration.
Updating the generator without regenerating the existing client files leaves the old incorrect types in place.
Why this matters to generated clients
Suppose the generated TypeScript client previously promised:
type MessageResponse = {
result: {
data: string;
};
};
Application code could access the value directly:
const message = response.result.data;
console.log(message.toUpperCase());
TypeScript sees a string and reports no problem.
But if the server returns { "result": {} }, the actual value is undefined.
Calling toUpperCase() then throws a runtime error.
With the corrected type:
type MessageResponse = {
result: {
data?: string;
};
};
the application must consider the missing value:
const message = response.result.data;
if (typeof message === "string") {
console.log(message.toUpperCase());
}
This is the behavior the original schema should have encouraged.
The new generator does not change the procedure’s runtime output. It corrects the description of that output.
What if the client actually requires data?
Not every optional response is desirable.
For some APIs, an absent result is part of normal behavior. A lookup may find nothing, and undefined may be an intentional outcome.
For others, the application expects a value on every successful response.
In that case, the better solution may be to make the server’s contract stricter.
For example:
const findMessage = t.procedure
.output(z.string())
.query(() => "Hello");
Now the output validator requires a string.
If a resolver returns an invalid value, tRPC’s output validation can reject it rather than silently accepting an unexpected result.
You could also choose an explicit nullable contract:
const findMessage = t.procedure
.output(z.string().nullable())
.query(() => null);
Here, the response can contain:
{
"result": {
"data": null
}
}
The field remains present, and its value communicates the absence of a result.
Which approach is appropriate depends on the API contract.
The important part is that the generated schema should describe the behavior the server actually permits.
What the November 27 fix changes
tRPC 11.32.2 does not introduce optional procedure outputs. Those already existed.
The change corrects how @trpc/openapi describes successful responses whose output types accept undefined.
Previously, generated clients could treat result.data as guaranteed, even when the server was allowed to omit it.
Now the generator distinguishes between an optional output and a required one.
For applications using OpenAPI-generated TypeScript clients, this may reveal places where application code relied on a property that was never guaranteed at runtime.
That may initially produce additional TypeScript errors after regeneration.
Those errors are useful: they identify assumptions that the previous generated types failed to catch.
The runtime behavior has not suddenly changed. The type definition has become more accurate.