upstash/context7 · error · Context7Error

Request did not return a result

Error message

Request did not return a result

What it means

Thrown by SearchLibraryCommand.exec() (the override) when client.request() returns result === undefined. The very next line dereferences result.results.map(...), so without this guard an undefined result would throw a less clear TypeError. Applies specifically to the v2/libs/search endpoint.

Source

Thrown at packages/sdk/src/commands/search-library/index.ts:36

    const queryParams: Record<string, string | number | undefined> = {};

    queryParams.query = query;
    queryParams.libraryName = libraryName;

    super({ method: "GET", query: queryParams }, "v2/libs/search");

    this.responseType = options?.type ?? DEFAULT_TYPE;
  }

  public override async exec(client: Requester): Promise<Library[] | string> {
    const { result } = await client.request<ApiSearchResponse>({
      method: this.request.method || "GET",
      path: [this.endpoint],
      query: this.request.query,
    });

    if (result === undefined) {
      throw new Context7Error("Request did not return a result");
    }

    const libraries = result.results.map(formatLibrary);

    if (this.responseType === "txt") {
      return formatLibrariesAsText(libraries);
    }

    return libraries;
  }
}

View on GitHub (pinned to ca15df0443)

Solutions

  1. Reproduce the search call with curl and inspect status, Content-Type, and body.
  2. In tests, make the mock Requester return { result: { results: [...] } }.
  3. Verify the API key and query params produce a real JSON search response.
  4. If the server legitimately returns empty, consider treating it as { results: [] } upstream after confirming the contract.

Example fix

// before
const mock = { request: async () => ({}) };
await cmd.exec(mock as any); // Context7Error

// after
const mock = { request: async () => ({ result: { results: [] } }) };
await cmd.exec(mock as any); // -> []
Defensive patterns

Strategy: type-guard

Validate before calling

import type { ApiSearchResponse } from "./types";
function isSearchResponse(v: unknown): v is ApiSearchResponse {
  return typeof v === "object" && v !== null && Array.isArray((v as any).results);
}
const res = await client.request<ApiSearchResponse>({ method: "GET", path: ["v2/libs/search"], query });
if (!isSearchResponse(res.result)) {
  throw new Error("v2/libs/search did not return a JSON body with a results array.");
}

Type guard

function isApiSearchResponse(v: unknown): v is { results: unknown[] } {
  return typeof v === "object" && v !== null && Array.isArray((v as any).results);
}

Try / catch

try {
  const libs = await searchCmd.exec(client);
} catch (e) {
  if (e instanceof Context7Error && /did not return a result/i.test(e.message)) {
    throw new Error("Search returned an empty body — confirm the API key and params, then retry.");
  }
  throw e;
}

Prevention

When it happens

Trigger: GET v2/libs/search returns 2xx with an undefined result (empty body, non-JSON content-type parsed to undefined, or a custom Requester returning { result: undefined }); 204 from a gateway; test mock missing the result field.

Common situations: Server/gateway returning an empty 200; proxy stripping the response body; unit test with an incomplete mock; a Response wrapper that resolves to undefined.

Related errors


AI-assisted analysis of upstash/context7@ca15df0443 (2026-08-12). Data as JSON: /api/errors/d38611759f05da8d. Report an issue: GitHub.