{"record":{"id":"a360577511a3100e","repo":"nats-io/nats-server","slug":"max-batch-must-not-be-negative","errorCode":null,"errorMessage":"max_batch must not be negative","messagePattern":"max_batch must not be negative","errorType":"validation","errorClass":"ApiError","httpStatus":null,"severity":"error","filePath":"server/consumer.go","lineNumber":634,"sourceCode":"\t\t}\n\t\tconfig.MaxDeliver = -1\n\t}\n\t// Setup zero defaults.\n\tif config.MaxWaiting < 0 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"max_waiting must not be negative\"))\n\t\t}\n\t\tconfig.MaxWaiting = 0\n\t}\n\tif config.MaxAckPending < -1 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"max_ack_pending must be set to -1\"))\n\t\t}\n\t\tconfig.MaxAckPending = -1\n\t}\n\tif config.MaxRequestBatch < 0 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"max_batch must not be negative\"))\n\t\t}\n\t\tconfig.MaxRequestBatch = 0\n\t}\n\tif config.MaxRequestExpires < 0 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"max_expires must not be negative\"))\n\t\t}\n\t\tconfig.MaxRequestExpires = 0\n\t}\n\tif config.MaxRequestMaxBytes < 0 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"max_bytes must not be negative\"))\n\t\t}\n\t\tconfig.MaxRequestMaxBytes = 0\n\t}\n\tif config.Heartbeat < 0 {\n\t\tif pedantic {\n\t\t\treturn NewJSPedanticError(errors.New(\"idle_heartbeat must not be negative\"))","sourceCodeStart":616,"sourceCodeEnd":652,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/consumer.go#L616-L652","documentation":"A JetStream pedantic validation error thrown when MaxRequestBatch (the max number of messages a pull request may batch) is negative. Batch sizes are counts and cannot be negative; in pedantic mode the server refuses the config rather than clamping it to 0 (server default). Thrown from server/consumer.go:634 during consumer create/update validation.","triggerScenarios":"ConsumerConfig.MaxRequestBatch < 0 passed to consumer creation/update with pedantic validation enabled, e.g. js.AddConsumer with MaxRequestBatch: -5 or a computed batch limit that went negative.","commonSituations":"Arithmetic like defaultBatch - extra producing a negative number; config flags such as --max-request-batch=-1; tenants copying other 'unlimited = -1' conventions from fields like MaxAckPending where -1 is legal but here it is not.","solutions":["Set MaxRequestBatch to a positive integer (e.g. 25_000) or 0 to use the server default.","Clamp computed values: if v < 0 { v = 0 } before assigning to MaxRequestBatch.","Remove any explicit negative value from consumer config YAML/JSON or CLI flags.","Run config.Validate(true) client-side before the request to surface it early."],"exampleFix":"// before\nbatchLimit := configuredBatch - overage // -3\ncc := nats.ConsumerConfig{MaxRequestBatch: batchLimit}\n// after\nif batchLimit < 0 { batchLimit = 0 }\ncc := nats.ConsumerConfig{MaxRequestBatch: batchLimit}","handlingStrategy":"validation","validationCode":"if cfg.MaxRequestBatch < 0 { return errors.New(\"max_batch must not be negative\") }","typeGuard":null,"tryCatchPattern":"if _, err := js.AddConsumer(stream, &cc); err != nil && strings.Contains(err.Error(), \"max_batch must not be negative\") {\n    cc.MaxRequestBatch = 0\n    _, err = js.AddConsumer(stream, &cc)\n}","preventionTips":["Use 0, not -1, to signal 'server default' for MaxRequestBatch.","Clamp any computed batch limits to >= 0 before assignment.","Validate configs client-side with Validate(true) in tests and at startup."],"tags":["jetstream","consumer-config","validation","pull-consumer"],"backgroundTag":"invalid-argument-value","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}