safe JsonContent
This commit is contained in:
parent
0cfb28fce7
commit
fa3f2cd0b6
|
|
@ -69,7 +69,19 @@ public static class StringExtensions
|
|||
AllowTrailingCommas = true
|
||||
};
|
||||
|
||||
return JsonSerializer.Deserialize<T>(text, options);
|
||||
// The payload usually comes from an LLM (tool-call arguments or a model's JSON answer) and is
|
||||
// occasionally not valid JSON — e.g. an unquoted first key (`{skill: "x"}`) or two objects
|
||||
// concatenated. A malformed payload must not throw out of a utility used across the whole
|
||||
// turn loop: it is treated as absent, so the caller keeps going instead of the whole
|
||||
// generation failing with a JsonException.
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T>(text, options);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public static string JsonArrayContent(this string text)
|
||||
|
|
@ -90,7 +102,16 @@ public static class StringExtensions
|
|||
AllowTrailingCommas = true
|
||||
};
|
||||
|
||||
return JsonSerializer.Deserialize<T[]>(text, options);
|
||||
// Same tolerance as JsonContent<T>: model-produced JSON is best-effort input, not a
|
||||
// contract, so invalid content yields no result rather than an unhandled exception.
|
||||
try
|
||||
{
|
||||
return JsonSerializer.Deserialize<T[]>(text, options);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return default;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsPrimitiveValue(this string value)
|
||||
|
|
|
|||
Loading…
Reference in a new issue