{"record":{"id":"db257a2310c01558","repo":"chroma-core/chroma","slug":"limit-must-be-a-positive-integer-when-provided","errorCode":null,"errorMessage":"Limit must be a positive integer when provided","messagePattern":"Limit must be a positive integer when provided","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"clients/new-js/packages/chromadb/src/execution/expression/limit.ts","lineNumber":21,"sourceCode":"  limit?: number | null | undefined;\n}\n\nexport type LimitInput = Limit | number | LimitOptions | null | undefined;\n\nexport class Limit {\n  public readonly offset: number;\n  public readonly limit?: number;\n\n  constructor(options: LimitOptions = {}) {\n    const { offset = 0, limit } = options;\n\n    if (!Number.isInteger(offset) || offset < 0) {\n      throw new TypeError(\"Limit offset must be a non-negative integer\");\n    }\n\n    if (limit !== null && limit !== undefined) {\n      if (!Number.isInteger(limit) || limit <= 0) {\n        throw new TypeError(\"Limit must be a positive integer when provided\");\n      }\n      this.limit = limit;\n    }\n\n    this.offset = offset;\n  }\n\n  public static from(input: LimitInput, offsetOverride?: number): Limit {\n    if (input instanceof Limit) {\n      return new Limit({ offset: input.offset, limit: input.limit });\n    }\n\n    if (typeof input === \"number\") {\n      return new Limit({ limit: input, offset: offsetOverride ?? 0 });\n    }\n\n    if (input === null || input === undefined) {\n      return new Limit();","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/chroma-core/chroma/blob/aecdd12c8a891610db8653630b066b32ceb678b5/clients/new-js/packages/chromadb/src/execution/expression/limit.ts#L3-L39","documentation":"When a limit value is provided (not null/undefined), the Limit constructor requires it to be a positive integer. limit: 0 is explicitly rejected — unlike offset, there is no 'zero means all' semantics; omit the limit entirely to leave it unbounded.","triggerScenarios":"new Limit({ limit: 0 }); new Limit({ limit: -5 }); new Limit({ limit: 10.5 }); or Limit.from(0). Fractional limits come from dividing counts (limit: total / 3).","commonSituations":"Application semantics where 0 means 'no cap' — in this API you must pass null/undefined instead. Size parameters read from config or query strings defaulting to 0. Percentage-based limits computed as fractions.","solutions":["To express 'no limit', omit the limit: new Limit({ offset }) or new Limit({ limit: undefined })","Clamp provided values: Math.max(1, Math.floor(Number(size)))","Validate external page-size input and reject/clamp 0 before constructing Limit"],"exampleFix":"// before\nnew Limit({ limit: size || 0 }); // size = 0 -> limit 0 -> TypeError\n\n// after\nnew Limit({ limit: size > 0 ? Math.floor(size) : undefined }); // undefined = unbounded","handlingStrategy":"validation","validationCode":"const size = Number(req.query.size);\nconst limit = new Limit({\n  offset,\n  limit: Number.isInteger(size) && size > 0 ? size : undefined, // undefined = unbounded\n});","typeGuard":"const isValidLimit = (v: unknown): v is number =>\n  typeof v === \"number\" && Number.isInteger(v) && v > 0;","tryCatchPattern":null,"preventionTips":["Use undefined/null to express 'no limit' — 0 is invalid","Clamp external sizes: Math.max(1, Math.floor(Number(x)))","Reject size=0 at request validation with a clear message"],"tags":["chroma","limit","pagination","numeric"],"backgroundTag":"invalid-numeric-argument","analyzedSha":"aecdd12c8a891610db8653630b066b32ceb678b5","analyzedAt":"2026-08-16T21:53:27.228Z","schemaVersion":2},"datasetVersion":"2026-08-16T23:17:17.608Z"}