{"record":{"id":"8f405b214a93aaff","repo":"dotnet/machinelearning","slug":"parameter-name-cannot-be-null","errorCode":null,"errorMessage":"Parameter name cannot be null","messagePattern":"Parameter name cannot be null","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.GenAI.Mistral/Mistral_7B_0_3ChatTemplateBuilder.cs","lineNumber":177,"sourceCode":"        var toolCallMessage = message.Message1;\n        var toolCallResultMessage = message.Message2;\n\n        var toolCall = BuildFromToolCallMessage(toolCallMessage);\n        var toolCallResult = BuildFromToolCallResultMessage(toolCallResultMessage);\n\n        return $\"{toolCall}{toolCallResult}\";\n    }\n\n    private JsonSchema BuildJsonSchemaFromFunctionContract(FunctionContract contract)\n    {\n        var requiredParameterNames = new List<string>();\n        var propertiesSchemas = new Dictionary<string, JsonSchema>();\n        var propertySchemaBuilder = new JsonSchemaBuilder().Type(SchemaValueType.Object);\n        foreach (var param in contract.Parameters ?? [])\n        {\n            if (param.Name is null)\n            {\n                throw new InvalidOperationException(\"Parameter name cannot be null\");\n            }\n\n            var schemaBuilder = new JsonSchemaBuilder().FromType(param.ParameterType ?? throw new ArgumentNullException(nameof(param.ParameterType)));\n            if (param.Description != null)\n            {\n                schemaBuilder = schemaBuilder.Description(param.Description);\n            }\n\n            if (param.IsRequired)\n            {\n                requiredParameterNames.Add(param.Name);\n            }\n\n            var schema = schemaBuilder.Build();\n            propertiesSchemas[param.Name] = schema;\n\n        }\n        propertySchemaBuilder = propertySchemaBuilder.Properties(propertiesSchemas);","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.GenAI.Mistral/Mistral_7B_0_3ChatTemplateBuilder.cs#L159-L195","documentation":"BuildJsonSchemaFromFunctionContract converts a FunctionContract's parameters into a JSON schema for Mistral tool-calling. If a parameter's Name is null the method throws InvalidOperationException('Parameter name cannot be null'), since a JSON-schema property requires a name.","triggerScenarios":"Passing a FunctionContract whose Parameters collection contains a KernelParameterMetadata/parameter descriptor with a null Name — typically a manually constructed contract or one deserialized from incomplete metadata.","commonSituations":"Hand-writing FunctionContract with parameter metadata where only Description/ParameterType were set; dynamic generation of contracts from reflected methods whose parameter names were lost (e.g. out params or trimmed assemblies).","solutions":["Set Name on every parameter of the FunctionContract before calling","Validate parameters before building: skip or fix any with null Name","If constructing contracts manually, use the constructor overload that requires a name"],"exampleFix":"// before\nvar param = new KernelParameterMetadata { ParameterType = typeof(string), Description = \"city\" };\n// after\nvar param = new KernelParameterMetadata { Name = \"city\", ParameterType = typeof(string), Description = \"city\" };","handlingStrategy":"validation","validationCode":"if (contract.Parameters?.Any(p => p.Name is null) == true)\n    throw new ArgumentException(\"All function parameters must have a Name before schema building\");","typeGuard":null,"tryCatchPattern":"try { schema = BuildJsonSchemaFromFunctionContract(contract); }\ncatch (InvalidOperationException ex) { log.Error($\"Bad parameter metadata on {contract.Name}\", ex); throw; }","preventionTips":["Always set Name when constructing parameter metadata","Validate contracts at registration time, not at request time","Avoid hand-building contracts from sources that lose parameter names"],"tags":["csharp","json-schema","tool-calling"],"backgroundTag":"null-argument","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}