microsoft/autogen · error · Exception

Function name cannot be null

Error message

Function name cannot be null

What it means

FunctionContractExtension.ToMistralFunctionDefinition converts an AutoGen FunctionContract (usually built from a .NET method via TypeProvideFunctionTools) into a Mistral FunctionDefinition, whose constructor requires a non-null name. A null functionContract.Name throws this generic Exception.

Source

Thrown at dotnet/src/AutoGen.Mistral/Extension/FunctionContractExtension.cs:21

using System;
using System.Collections.Generic;
using AutoGen.Core;
using Json.Schema;
using Json.Schema.Generation;

namespace AutoGen.Mistral.Extension;

public static class FunctionContractExtension
{
    /// <summary>
    /// Convert a <see cref="FunctionContract"/> to a <see cref="FunctionDefinition"/> that can be used in funciton call.
    /// </summary>
    /// <param name="functionContract">function contract</param>
    /// <returns><see cref="FunctionDefinition"/></returns>
    public static FunctionDefinition ToMistralFunctionDefinition(this FunctionContract functionContract)
    {
        var functionDefinition = new FunctionDefinition(functionContract.Name ?? throw new Exception("Function name cannot be null"), functionContract.Description ?? throw new Exception("Function description cannot be null"));
        var requiredParameterNames = new List<string>();
        var propertiesSchemas = new Dictionary<string, JsonSchema>();
        var propertySchemaBuilder = new JsonSchemaBuilder().Type(SchemaValueType.Object);
        foreach (var param in functionContract.Parameters ?? [])
        {
            if (param.Name is null)
            {
                throw new InvalidOperationException("Parameter name cannot be null");
            }

            var schemaBuilder = new JsonSchemaBuilder().FromType(param.ParameterType ?? throw new ArgumentNullException(nameof(param.ParameterType)));
            if (param.Description != null)
            {
                schemaBuilder = schemaBuilder.Description(param.Description);
            }

            if (param.IsRequired)
            {

View on GitHub (pinned to 027ecf0a37)

Solutions

  1. Set FunctionContract.Name (normally the method name) before converting.
  2. Prefer the official reflection-based helpers (e.g. FunctionContract.FromMethod / TypeProvideFunctionTools) so name extraction is automatic.
  3. Add a startup assertion over your tool list: any contract with null Name/Description fails fast with a clear message.

Example fix

// before
var contract = new FunctionContract { Description = "Gets weather" };
chatRequest.Tools = [contract.ToMistralFunctionDefinition()];

// after
var contract = new FunctionContract { Name = nameof(GetWeather), Description = "Gets weather", Parameters = ... };
chatRequest.Tools = [contract.ToMistralFunctionDefinition()];
Defensive patterns

Strategy: validation

Validate before calling

foreach (var contract in functionContracts)
{
    if (string.IsNullOrEmpty(contract.Name)) throw new ArgumentException($"Contract '{contract.Description}' has no Name");
}

Type guard

static bool HasValidName(FunctionContract c) => !string.IsNullOrEmpty(c.Name);

Prevention

When it happens

Trigger: Registering a function/tool whose FunctionContract has a null Name — e.g. a method whose name could not be resolved, a hand-built FunctionContract without Name, or a dynamically generated contract with missing metadata.

Common situations: Creating FunctionContract manually and forgetting Name; lambda/local methods or interop cases where the name extraction fails; null-propagating refactor of contract-building code.

Related errors


AI-assisted analysis of microsoft/autogen@027ecf0a37 (2026-08-15). Data as JSON: /api/errors/e5255aed3bff1a63. Report an issue: GitHub.