{"record":{"id":"e966c50f51f786c5","repo":"microsoft/semantic-kernel","slug":"maxtokens-maxtokens-is-not-valid-the-value-must-e966c5","errorCode":null,"errorMessage":"MaxTokens {maxTokens} is not valid, the value must be greater than zero","messagePattern":"MaxTokens (.+?) is not valid, the value must be greater than zero","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceClient.cs","lineNumber":61,"sourceCode":"        {\n            throw new InvalidOperationException(\"A valid model id or endpoint must be provided.\");\n        }\n\n        endpoint ??= new Uri(\"https://api-inference.huggingface.co\");\n        this.Separator = endpoint.AbsolutePath.EndsWith(\"/\", StringComparison.InvariantCulture) ? string.Empty : \"/\";\n        this.Endpoint = endpoint;\n        this.ModelId = modelId;\n        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","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.HuggingFace/Core/HuggingFaceClient.cs#L43-L79","documentation":"Thrown by HuggingFaceClient.ValidateMaxTokens when the maxTokens setting is less than 1 (a negative value or zero). Text-generation max token counts must be positive. Used by the older text-generation request path (MaxTokens).","triggerScenarios":"Setting MaxTokens to 0 or a negative number on HuggingFacePromptExecutionSettings for a text-generation (non-chat) request and invoking the service.","commonSituations":"Passing MaxTokens from an unvalidated user/config value of 0; confusing MaxTokens (>=1) with MaxNewTokens (>=0); arithmetic that underflows to a negative.","solutions":["Set MaxTokens to a positive integer (>=1) or leave it null to use the model default.","Validate configuration before invoking: reject values < 1.","If you meant the chat-style setting, use MaxNewTokens instead (which allows 0)."],"exampleFix":"// before\nvar settings = new HuggingFacePromptExecutionSettings { MaxTokens = 0 };  // throws\n\n// after\nvar settings = new HuggingFacePromptExecutionSettings { MaxTokens = 100 };\n// or omit to use default\nvar settings = new HuggingFacePromptExecutionSettings();","handlingStrategy":"validation","validationCode":"static int? ClampMaxTokens(int? v) => v is < 1 ? throw new ArgumentException(\"MaxTokens must be >= 1\") : v;","typeGuard":"static bool IsValidMaxTokens(int? v) => v is null or >= 1;","tryCatchPattern":null,"preventionTips":["Validate MaxTokens >= 1 before invoking.","Leave MaxTokens null to use the model default.","Don't confuse MaxTokens (>=1) with MaxNewTokens (>=0)."],"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"}