{"record":{"id":"0d14128b9151e260","repo":"microsoft/semantic-kernel","slug":"invalid-key-value-pair-format-inputpair-expect","errorCode":null,"errorMessage":"Invalid key-value pair format: {inputPair}; expecting \"{keyName}/{valueName}\"","messagePattern":"Invalid key-value pair format: (.+?); expecting \"(.+?)/(.+?)\"","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"dotnet/src/Agents/Runtime/Abstractions/Internal/KeyValueParserExtensions.cs","lineNumber":50,"sourceCode":"    /// </exception>\n    /// <example>\n    /// Example usage:\n    /// <code>\n    /// string input = \"agent1/12345\";\n    /// var result = input.ToKVPair(\"Type\", \"Key\");\n    /// Console.WriteLine(result.Item1); // Outputs: agent1\n    /// Console.WriteLine(result.Item2); // Outputs: 12345\n    /// </code>\n    /// </example>\n    public static (string, string) ToKeyValuePair(this string inputPair, string keyName, string valueName)\n    {\n        Match match = KVPairRegex.Match(inputPair);\n        if (match.Success)\n        {\n            return (match.Groups[\"key\"].Value, match.Groups[\"value\"].Value);\n        }\n\n        throw new FormatException($\"Invalid key-value pair format: {inputPair}; expecting \\\"{{{keyName}}}/{{{valueName}}}\\\"\");\n    }\n}\n","sourceCodeStart":32,"sourceCodeEnd":53,"githubUrl":"https://github.com/microsoft/semantic-kernel/blob/c028a0c7dc4f0814cdcbaba9d998f187a41197bf/dotnet/src/Agents/Runtime/Abstractions/Internal/KeyValueParserExtensions.cs#L32-L53","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only call FromStr with strings in `\\w+/\\w+` form; keep AgentId keys to word characters if you intend to round-trip via FromStr.","If you must parse keys with broader characters, split manually and construct `new AgentId(type, key)` instead of FromStr.","Validate the format with the same regex before parsing."],"exampleFix":"// before\nvar id = AgentId.FromStr(raw); // raw may be \"type/my-key\" -> fails\n\n// after\nvar parts = raw.Split('/', 2);\nvar id = new AgentId(parts[0], parts[1]); // accepts broader key charset","handlingStrategy":"validation","validationCode":"static readonly Regex KvPair = new(@\"^(?<key>\\w+)/(?<value>\\w+)$\", RegexOptions.Compiled);\nbool IsParsableAgentIdStr(string s) => !string.IsNullOrEmpty(s) && KvPair.IsMatch(s);\n// If your key may contain non-word chars, parse manually instead of FromStr:\nAgentId ParseLoose(string s)\n{\n    var p = s.Split('/', 2);\n    return new AgentId(p[0], p[1]);\n}","typeGuard":"bool IsAgentIdStr(string s) => Regex.IsMatch(s ?? \"\", @\"^\\w+/\\w+$\");","tryCatchPattern":null,"preventionTips":["Keep AgentId keys to word characters if you round-trip via FromStr.","Prefer manual split + `new AgentId(type, key)` for keys with broader characters.","Validate the `type/key` format before parsing user input."],"tags":["agentid","parsing","format","runtime","dotnet"],"backgroundTag":null,"analyzedSha":"c028a0c7dc4f0814cdcbaba9d998f187a41197bf","analyzedAt":"2026-08-13T13:48:05.040Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}