{"record":{"id":"a7291dd28bf1f0fc","repo":"microsoft/autogen","slug":"value-cannot-be-null-parameter-functioncontract","errorCode":null,"errorMessage":"Value cannot be null. (Parameter 'functionContract.Name')","messagePattern":"Value cannot be null\\. \\(Parameter 'functionContract\\.Name'\\)","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"dotnet/src/AutoGen.OpenAI/Extension/FunctionContractExtension.cs","lineNumber":55,"sourceCode":"            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);\n        propertySchemaBuilder = propertySchemaBuilder.Required(requiredParameterNames);\n\n        var option = new System.Text.Json.JsonSerializerOptions()\n        {\n            PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase\n        };\n\n        var functionDefinition = ChatTool.CreateFunctionTool(\n           functionContract.Name ?? throw new ArgumentNullException(nameof(functionContract.Name)),\n           functionContract.Description,\n           BinaryData.FromObjectAsJson(propertySchemaBuilder.Build(), option));\n\n        return functionDefinition;\n    }\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    [Obsolete(\"Use ToChatTool instead\")]\n    public static ChatTool ToOpenAIFunctionDefinition(this FunctionContract functionContract)\n    {\n        return functionContract.ToChatTool();\n    }\n}\n","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/dotnet/src/AutoGen.OpenAI/Extension/FunctionContractExtension.cs#L37-L73","documentation":"ArgumentNullException ('functionContract.Name') thrown by ChatTool.CreateFunctionTool inside ToChatTool when the FunctionContract has a null Name. The OpenAI tools API requires every function tool to have a name, so a nameless contract cannot be converted.","triggerScenarios":"Calling ToChatTool() on a FunctionContract constructed without setting Name; contract built from reflection where the method name could not be resolved.","commonSituations":"Hand-authored contracts with a typo (e.g. setting Description but not Name); dynamic method wrappers that lose the method name; test fixtures constructed incompletely.","solutions":["Set FunctionContract.Name to a valid function name (letters, digits, underscores, <=64 chars)","Create contracts via TypeReflector.GetFunctionContract so Name comes from the delegate/method","Null-check contract.Name before registering tools and throw a clearer error at your boundary"],"exampleFix":"// before\nvar contract = new FunctionContract { Description = \"Gets weather\" }; // Name missing\ncontract.ToChatTool(); // ArgumentNullException\n\n// after\nvar contract = new FunctionContract { Name = \"get_weather\", Description = \"Gets weather\", ... };","handlingStrategy":"validation","validationCode":"if (string.IsNullOrWhiteSpace(contract.Name)) { /* assign a valid function name before ToChatTool */ }","typeGuard":"static bool ContractHasName(FunctionContract c) => !string.IsNullOrWhiteSpace(c.Name);","tryCatchPattern":"catch (ArgumentNullException ex) when (ex.ParamName == \"functionContract.Name\") { /* set Name and re-register */ }","preventionTips":["Name every FunctionContract at construction","Derive contracts from real delegates via TypeReflector","Validate at 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"}