{"record":{"id":"e6e921b9c2b04e15","repo":"microsoft/autogen","slug":"parameter-name-cannot-be-null-e6e921","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.V1/Extension/FunctionContractExtension.cs","lineNumber":33,"sourceCode":"    /// Convert a <see cref=\"FunctionContract\"/> to a <see cref=\"FunctionDefinition\"/> that can be used in gpt funciton call.\n    /// </summary>\n    /// <param name=\"functionContract\">function contract</param>\n    /// <returns><see cref=\"FunctionDefinition\"/></returns>\n    public static FunctionDefinition ToOpenAIFunctionDefinition(this FunctionContract functionContract)\n    {\n        var functionDefinition = new FunctionDefinition\n        {\n            Name = functionContract.Name,\n            Description = functionContract.Description,\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 ArgumentException(\"param.ParameterType cannot be null\"));\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":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI.V1/Extension/FunctionContractExtension.cs#L15-L51","documentation":"FunctionContractExtension.ToOpenAIFunctionDefinition throws InvalidOperationException while converting a FunctionContract to an OpenAI FunctionDefinition when a parameter has a null Name. JSON-schema object properties are keyed by parameter name, so a nameless parameter cannot be represented.","triggerScenarios":"Registering a function/TypeTool whose contract was built with a parameter entry where Name was never assigned (e.g. hand-constructed FunctionContract.Parameters, or a custom contract builder bug).","commonSituations":"Manually authoring FunctionContract instances instead of deriving them from CLR methods; deserializing contracts from JSON where the name field casing didn't bind; partially-implemented IFunctionContract providers.","solutions":["Give every parameter in functionContract.Parameters a non-null, unique Name","Prefer building contracts from real .NET methods (the built-in reflection-based contract provider) so names are always populated","Add a unit test asserting all parameter names are non-empty when contracts are created dynamically"],"exampleFix":"// before\nvar contract = new FunctionContract\n{\n    Name = \"get_weather\",\n    Parameters = new[] { new FunctionParameter { ParameterType = typeof(string) } } // Name missing\n};\n\n// after\nvar contract = new FunctionContract\n{\n    Name = \"get_weather\",\n    Parameters = new[] { new FunctionParameter { Name = \"city\", ParameterType = typeof(string), IsRequired = true } }\n};","handlingStrategy":"validation","validationCode":"bool ContractIsValid(FunctionContract c) =>\n    (c.Parameters ?? []).All(p => !string.IsNullOrEmpty(p.Name));","typeGuard":"static bool HasNamedParameters(FunctionContract c) =>\n    c.Parameters?.All(p => p.Name is not null) ?? true;","tryCatchPattern":"catch (InvalidOperationException ex) when (ex.Message == \"Parameter name cannot be null\")\n{\n    throw new ArgumentException($\"Function {fn.Name} has a parameter without a name\", ex);\n}","preventionTips":["Build contracts from CLR methods where possible","Validate contracts in unit tests before registration","Check JSON field casing when deserializing contracts"],"tags":["openai","function-calling","function-contract","validation"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}