microsoft/semantic-kernel · error · FormatException

Invalid key-value pair format: {inputPair}; expecting "{keyN

Error message

Invalid key-value pair format: {inputPair}; expecting "{keyName}/{valueName}"

What it means

KeyValueParserExtensions.ToKeyValuePair parses `key/value` strings using the pattern `^(?<key>[word-chars]+)/(?<value>[word-chars]+)$` (word characters: letters/digits/underscore) and throws FormatException on mismatch. It is used internally by AgentId.FromStr (and the explicit string-to-AgentId operator) to parse `"type/key"`. Both sides must be word characters with a single slash separator.

Source

Thrown at dotnet/src/Agents/Runtime/Abstractions/Internal/KeyValueParserExtensions.cs:50

    /// </exception>
    /// <example>
    /// Example usage:
    /// <code>
    /// string input = "agent1/12345";
    /// var result = input.ToKVPair("Type", "Key");
    /// Console.WriteLine(result.Item1); // Outputs: agent1
    /// Console.WriteLine(result.Item2); // Outputs: 12345
    /// </code>
    /// </example>
    public static (string, string) ToKeyValuePair(this string inputPair, string keyName, string valueName)
    {
        Match match = KVPairRegex.Match(inputPair);
        if (match.Success)
        {
            return (match.Groups["key"].Value, match.Groups["value"].Value);
        }

        throw new FormatException($"Invalid key-value pair format: {inputPair}; expecting \"{{{keyName}}}/{{{valueName}}}\"");
    }
}

View on GitHub (pinned to c028a0c7dc)

Solutions

  1. Only call FromStr with strings in `\w+/\w+` form; keep AgentId keys to word characters if you intend to round-trip via FromStr.
  2. If you must parse keys with broader characters, split manually and construct `new AgentId(type, key)` instead of FromStr.
  3. Validate the format with the same regex before parsing.

Example fix

// before
var id = AgentId.FromStr(raw); // raw may be "type/my-key" -> fails

// after
var parts = raw.Split('/', 2);
var id = new AgentId(parts[0], parts[1]); // accepts broader key charset
Defensive patterns

Strategy: validation

Validate before calling

static readonly Regex KvPair = new(@"^(?<key>\w+)/(?<value>\w+)$", RegexOptions.Compiled);
bool IsParsableAgentIdStr(string s) => !string.IsNullOrEmpty(s) && KvPair.IsMatch(s);
// If your key may contain non-word chars, parse manually instead of FromStr:
AgentId ParseLoose(string s)
{
    var p = s.Split('/', 2);
    return new AgentId(p[0], p[1]);
}

Type guard

bool IsAgentIdStr(string s) => Regex.IsMatch(s ?? "", @"^\w+/\w+$");

Prevention

When it happens

Trigger: Calling AgentId.FromStr("...") or `(AgentId)"..."` with a string that has no slash, multiple slashes, non-word characters on either side, or is empty. Even a valid AgentId key containing a dash or space will fail FromStr, because FromStr's parser is stricter than the AgentId key validator.

Common situations: Round-tripping an AgentId whose key contains a dash/space through FromStr (ToString() works, FromStr() can fail); parsing user-supplied identifiers; splitting on the wrong delimiter.

Related errors


AI-assisted analysis of microsoft/semantic-kernel@c028a0c7dc (2026-08-13). Data as JSON: /api/errors/0d14128b9151e260. Report an issue: GitHub.