{"record":{"id":"730d77277a2af225","repo":"microsoft/autogen","slug":"parameter-name-cannot-be-null-730d77","errorCode":null,"errorMessage":"Parameter name cannot be null","messagePattern":"Parameter name cannot be null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Extension/FunctionContractExtension.cs","lineNumber":28,"sourceCode":"namespace AutoGen.OpenAI.Extension;\n\npublic static class FunctionContractExtension\n{\n    /// <summary>\n    /// Convert a <see cref=\"FunctionContract\"/> to a <see cref=\"ChatTool\"/> that can be used in gpt funciton call.\n    /// </summary>\n    /// <param name=\"functionContract\">function contract</param>\n    /// <returns><see cref=\"ChatTool\"/></returns>\n    public static ChatTool ToChatTool(this FunctionContract functionContract)\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 functionContract.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":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Extension/FunctionContractExtension.cs#L10-L46","documentation":"InvalidOperationException thrown in FunctionContractExtension.ToChatTool when a FunctionContract parameter has a null Name. Each parameter must become a named property in the tool's JSON schema, so a nameless parameter cannot be represented.","triggerScenarios":"A FunctionContract whose Parameters collection contains a FunctionParameter with Name == null — usually from hand-built contracts, custom TypeReflector implementations, or plugin loading that failed to resolve parameter names.","commonSituations":"Manually authoring FunctionContract instead of deriving it from a delegate via TypeReflector; dynamic plugin loaders that lose parameter metadata; obfuscated/reflected methods where parameter names are unavailable.","solutions":["Use TypeReflector.GetFunctionContract(delegate/methodInfo) so parameter names are populated automatically","Ensure every FunctionParameter in a hand-built contract has a non-null Name","Validate the contract before registration (assert param.Name is not null) to fail with better context"],"exampleFix":"// before\nvar contract = new FunctionContract\n{\n    Name = \"get_weather\",\n    Parameters = [new FunctionParameter { Name = null, ParameterType = typeof(string) }],\n};\ntool = contract.ToChatTool(); // throws\n\n// after\nParameters = [new FunctionParameter { Name = \"city\", ParameterType = typeof(string), IsRequired = true }],","handlingStrategy":"validation","validationCode":"foreach (var p in contract.Parameters ?? [])\n{\n    if (p.Name is null) throw new ArgumentException($\"Parameter '{p}' of function '{contract.Name}' has no name\");\n}","typeGuard":"static bool ContractHasNamedParameters(FunctionContract c) => (c.Parameters ?? []).All(p => p.Name is not null);","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message == \"Parameter name cannot be null\") { /* fix contract and re-register tool */ }","preventionTips":["Prefer TypeReflector.GetFunctionContract over hand-built contracts","Validate contracts once at tool-registration time"],"tags":["autogen","dotnet","openai","function-calling","function-contract","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}