{"record":{"id":"d4f1bc572f21639a","repo":"microsoft/semantic-kernel","slug":"maxnewtokens-maxnewtokens-is-not-valid-the-valu","errorCode":null,"errorMessage":"MaxNewTokens {maxNewTokens} is not valid, the value must be greater than or equal to zero","messagePattern":"MaxNewTokens (.+?) is not valid, the value must be greater than or equal to zero","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceClient.cs","lineNumber":69,"sourceCode":"        this.ApiKey = apiKey;\n        this._httpClient = httpClient;\n        this.Logger = logger ?? NullLogger.Instance;\n    }\n\n    #region ClientCore\n    internal static void ValidateMaxTokens(int? maxTokens)\n    {\n        if (maxTokens is < 1)\n        {\n            throw new ArgumentException($\"MaxTokens {maxTokens} is not valid, the value must be greater than zero\");\n        }\n    }\n\n    internal static void ValidateMaxNewTokens(int? maxNewTokens)\n    {\n        if (maxNewTokens is < 0)\n        {\n            throw new ArgumentException($\"MaxNewTokens {maxNewTokens} is not valid, the value must be greater than or equal to zero\");\n        }\n    }\n\n    internal async Task<string> SendRequestAndGetStringBodyAsync(\n        HttpRequestMessage httpRequestMessage,\n        CancellationToken cancellationToken)\n    {\n        using var response = await this._httpClient.SendWithSuccessCheckAsync(httpRequestMessage, cancellationToken)\n            .ConfigureAwait(false);\n\n        var body = await response.Content.ReadAsStringWithExceptionMappingAsync(cancellationToken)\n            .ConfigureAwait(false);\n\n        return body;\n    }\n\n    internal async Task<HttpResponseMessage> SendRequestAndGetResponseImmediatelyAfterHeadersReadAsync(\n        HttpRequestMessage httpRequestMessage,","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceClient.cs#L51-L87","documentation":"Thrown by HuggingFaceClient.ValidateMaxNewTokens when maxNewTokens is less than 0 (a negative value). maxNewTokens may be 0 (generate nothing new) but cannot be negative. Used by the chat/message request path.","triggerScenarios":"Setting MaxNewTokens to a negative number on HuggingFacePromptExecutionSettings and invoking a chat or text-generation service that uses the new-tokens parameter.","commonSituations":"Passing an unvalidated negative value from config; subtracting budgets that underflow below zero; misreading the bound (it allows 0, not 1).","solutions":["Set MaxNewTokens to 0 or a positive integer, or leave it null for the default.","Clamp user-supplied values to max(0, value) before assigning.","Check for arithmetic that can produce negatives (e.g. budget - used)."],"exampleFix":"// before\nvar settings = new HuggingFacePromptExecutionSettings { MaxNewTokens = -5 };  // throws\n\n// after\nvar settings = new HuggingFacePromptExecutionSettings { MaxNewTokens = Math.Max(0, requested) };","handlingStrategy":"validation","validationCode":"static int? ClampMaxNewTokens(int? v) => v is < 0 ? throw new ArgumentException(\"MaxNewTokens must be >= 0\") : v;","typeGuard":"static bool IsValidMaxNewTokens(int? v) => v is null or >= 0;","tryCatchPattern":null,"preventionTips":["Validate MaxNewTokens >= 0 before invoking.","Clamp user input with Math.Max(0, value).","Remember 0 is allowed for MaxNewTokens, unlike MaxTokens."],"tags":["huggingface","configuration","validation","max-tokens"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}