upstash/context7 · error · Context7Error
Request did not return a result
Error message
Request did not return a result
What it means
Thrown by GetContextCommand.exec() (the override) when client.request() returns result === undefined. Same root cause as the base guard but throws Context7Error instead of TypeError, and applies specifically to the v2/context endpoint. After this check the code dereferences apiResult.codeSnippets / infoSnippets, so the guard prevents a downstream TypeError on undefined.
Source
Thrown at packages/sdk/src/commands/get-context/index.ts:36
const responseType = options?.type ?? DEFAULT_TYPE;
queryParams.type = responseType;
super({ method: "GET", query: queryParams }, "v2/context");
this.responseType = responseType;
}
public override async exec(client: Requester): Promise<Documentation[] | string> {
const { result } = await client.request<string | ApiContextJsonResponse>({
method: this.request.method || "GET",
path: [this.endpoint],
query: this.request.query,
body: this.request.body,
});
if (result === undefined) {
throw new Context7Error("Request did not return a result");
}
if (this.responseType === "txt" && typeof result === "string") {
return result;
}
const apiResult = result as ApiContextJsonResponse;
const codeDocs = apiResult.codeSnippets.map(formatCodeSnippet);
const infoDocs = apiResult.infoSnippets.map(formatInfoSnippet);
return [...codeDocs, ...infoDocs];
}
}
View on GitHub (pinned to ca15df0443)
Solutions
- Reproduce the GET v2/context call directly and inspect status, Content-Type, and body.
- In tests, make the mock Requester return a full ApiContextJsonResponse (with codeSnippets and infoSnippets arrays).
- If the server genuinely returns empty, handle it upstream (return [] instead of throwing) — but only after confirming the contract.
- Verify the endpoint URL and query params are well-formed so the server actually returns context.
Example fix
// before
const mock = { request: async () => ({}) };
await cmd.exec(mock as any); // Context7Error
// after
const mock = {
request: async () => ({
result: { codeSnippets: [], infoSnippets: [] },
}),
};
await cmd.exec(mock as any); // -> [] Defensive patterns
Strategy: type-guard
Validate before calling
import type { ApiContextJsonResponse } from "./types";
function isContextResponse(v: unknown): v is ApiContextJsonResponse {
return (
typeof v === "object" && v !== null &&
Array.isArray((v as any).codeSnippets) &&
Array.isArray((v as any).infoSnippets)
);
}
const res = await client.request<ApiContextJsonResponse>({ method: "GET", path: ["v2/context"], query });
if (!isContextResponse(res.result)) {
throw new Error("v2/context did not return a JSON context body (missing codeSnippets/infoSnippets).");
} Type guard
function isApiContextJsonResponse(v: unknown): v is { codeSnippets: unknown[]; infoSnippets: unknown[] } {
return typeof v === "object" && v !== null &&
Array.isArray((v as any).codeSnippets) && Array.isArray((v as any).infoSnippets);
} Try / catch
import { Context7Error } from "@error";
try {
const docs = await getContextCmd.exec(client);
} catch (e) {
if (e instanceof Context7Error && /did not return a result/i.test(e.message)) {
throw new Error("v2/context returned an empty body — confirm the endpoint and responseType, then retry.");
}
throw e;
} Prevention
- In tests, have the mock Requester return a full ApiContextJsonResponse with both snippet arrays.
- Confirm v2/context responses are application/json with a non-empty body.
- Validate the response shape with isApiContextJsonResponse before dereferencing its fields.
When it happens
Trigger: GET v2/context returns 2xx but with an undefined body/result (empty body with non-JSON content-type, or a custom Requester returning { result: undefined }); a 204 No Content from a misconfigured gateway.
Common situations: Server bug returning 200 with empty body; proxy stripping the body; test mock missing the result field; wrong responseType configuration causing the HTTP layer to bypass JSON parsing.
Related errors
- Request did not return a result
- Request did not return a result
- errorBody.error || errorBody.message || res.statusText
- Failed to fetch user info
- ${fallback} (HTTP ${response.status} from ${response.url}):
AI-assisted analysis of upstash/context7@ca15df0443 (2026-08-12).
Data as JSON: /api/errors/db38180f471e1d7f.
Report an issue: GitHub.