apify/crawlee · error · Error

Cannot create Request from type: ${type}

Error message

Cannot create Request from type: ${type}

What it means

RequestList.addRequest()/addRequests() accepts sources as strings (URLs), Request instances, or plain objects (RequestOptions). When the type of a source item is none of the recognized kinds, the library cannot construct a Request and throws with the detected typeof.

Source

Thrown at packages/core/src/storages/request_list.ts:748

    }

    /**
     * Adds given request.
     * If the `source` parameter is a string or plain object and not an instance
     * of a `Request`, then the function creates a `Request` instance.
     */
    private addRequest(source: RequestListSource) {
        let request: Request | RequestOptions;
        const type = typeof source;

        if (type === 'string') {
            request = { url: source as string };
        } else if (source instanceof Request) {
            request = source;
        } else if (source && type === 'object') {
            request = source as RequestOptions;
        } else {
            throw new Error(`Cannot create Request from type: ${type}`);
        }

        const hasUniqueKey = Reflect.has(Object(source), 'uniqueKey');
        request.uniqueKey ??= Request.computeUniqueKey(request as any);

        // Add index to uniqueKey if duplicates are to be kept
        if (this.#keepDuplicateUrls && !hasUniqueKey) {
            request.uniqueKey += `-${this.requests.length}`;
        }

        const { uniqueKey } = request;
        this.ensureUniqueKeyValid(uniqueKey);

        // Skip requests with duplicate uniqueKey
        if (!Object.hasOwn(this.#uniqueKeyToIndex, uniqueKey)) {
            this.#uniqueKeyToIndex[uniqueKey] = this.requests.length;
            this.requests.push(request);
        } else if (this.#keepDuplicateUrls) {

View on GitHub (pinned to dbe57fb09c)

Solutions

  1. Ensure every source item is a URL string, a Request instance, or an object with at least a url property
  2. Filter/validate the array before passing it to addRequests
  3. Fix data mapping that introduces undefined/null items
  4. Enable strict TypeScript typing on the sources array (Array<string | Request | RequestOptions>)

Example fix

// before
await requestList.addRequests([undefined, 'https://x.com']);
// after
const sources = [undefined, 'https://x.com'].filter((s): s is string => typeof s === 'string');
await requestList.addRequests(sources);
Defensive patterns

Strategy: type-guard

Validate before calling

const isSource = (s) => typeof s === 'string' || s instanceof Request || (s && typeof s === 'object' && typeof s.url === 'string');
sources = sources.filter(isSource);

Type guard

function isRequestSource(s) {
  return typeof s === 'string'
    || s instanceof Request
    || (s !== null && typeof s === 'object' && typeof (s).url === 'string');
}

Try / catch

try {
  await requestList.addRequests(sources);
} catch (err) {
  if (err.message.startsWith('Cannot create Request from type')) {
    logger.error(`Bad source item: ${err.message}`);
  } else throw err;
}

Prevention

When it happens

Trigger: Passing an array of addRequests containing items that are numbers, booleans, null, undefined, arrays, or other unsupported values; passing a non-Request class instance that is neither string nor plain object.

Common situations: Mapping over data and accidentally including undefined/null entries; spreading a parsed JSON where items are nested objects with wrong shape (missing url so treated as type mismatch upstream); TypeScript types bypassed with `as any`.

Related errors


AI-assisted analysis of apify/crawlee@dbe57fb09c (2026-08-30). Data as JSON: /api/errors/fbad12c6e3abee60. Report an issue: GitHub.