{"record":{"id":"5b9dc9ce0559f81e","repo":"microsoft/semantic-kernel","slug":"maxtokens-maxtokens-is-not-valid-the-value-must-5b9dc9","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.OpenAI/Core/ClientCore.ChatCompletion.cs","lineNumber":1100,"sourceCode":"                    id: toolCall.Id,\n                    arguments: arguments)\n                {\n                    InnerContent = toolCall,\n                    Exception = exception\n                };\n\n                result.Add(functionCallContent);\n            }\n        }\n\n        return result;\n    }\n\n    private static void ValidateMaxTokens(int? maxTokens)\n    {\n        if (maxTokens.HasValue && maxTokens < 1)\n        {\n            throw new ArgumentException($\"MaxTokens {maxTokens} is not valid, the value must be greater than zero\");\n        }\n    }\n\n    /// <summary>\n    /// Gets the response modalities from the execution settings.\n    /// </summary>\n    /// <param name=\"executionSettings\">The execution settings.</param>\n    /// <returns>The response modalities as a <see cref=\"ChatResponseModalities\"/> flags enum.</returns>\n    /// <remarks>\n    /// This method supports converting from various formats:\n    /// <list type=\"bullet\">\n    /// <item><description>A <see cref=\"ChatResponseModalities\"/> flags enum</description></item>\n    /// <item><description>A string representation of the enum (e.g., \"Text, Audio\")</description></item>\n    /// <item><description>An <see cref=\"IEnumerable{String}\"/> of modality names (e.g., [\"text\", \"audio\"])</description></item>\n    /// <item><description>A <see cref=\"JsonElement\"/> containing either a string, or array of strings</description></item>\n    /// </list>\n    /// </remarks>\n    private static ChatResponseModalities GetResponseModalities(OpenAIPromptExecutionSettings executionSettings)","sourceCodeStart":1082,"sourceCodeEnd":1118,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Connectors/Connectors.OpenAI/Core/ClientCore.ChatCompletion.cs#L1082-L1118","documentation":"Thrown by ValidateMaxTokens. MaxTokens must be a positive integer when set; a value of zero or negative is invalid because OpenAI's max_completion_tokens/max_tokens field requires >= 1. The check runs before the request is sent so the API never sees a malformed value.","triggerScenarios":"Setting executionSettings.MaxTokens to 0 or a negative number (e.g. from a config default of 0, a computed value that underflowed, or deserialized -1 meaning 'unlimited').","commonSituations":"Treating 0/-1 as 'no limit' (the connector uses null for that); config with max_tokens:0; arithmetic that produced a non-positive budget.","solutions":["Set MaxTokens to a positive integer, or leave it null to let the model decide.","If 'unlimited' was intended, pass null rather than 0.","Clamp computed values to at least 1 before assigning."],"exampleFix":"// before\nsettings.MaxTokens = 0; // or -1\n// after\nsettings.MaxTokens = null; // or a positive int like 1024","handlingStrategy":"validation","validationCode":"static int? NormalizeMaxTokens(int? v) => v switch { null => null, > 0 => v, _ => throw new ArgumentException(\"MaxTokens must be > 0; use null for unlimited\") };","typeGuard":"static bool IsValidMaxTokens(int? v) => v is null or > 0;","tryCatchPattern":"try { await client.GetChatCompletionAsync(...); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"MaxTokens\")) { settings.MaxTokens = null; /* retry */ }","preventionTips":["Use null to mean 'no limit', never 0 or -1.","Clamp computed budgets to >= 1."],"tags":["openai","chat-completion","max-tokens","validation"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}