Task.WhenAll with Result types: catch one failed LLM call of two
When you run several LLM calls in parallel with Task.WhenAll and each call returns a Result value instead of throwing, a single failure can disappear. If the caller maps a failed Result to an empty value and then decides the run's status by asking "did we write anything?", a run where one call of two failed still writes the other half and reports Success. The fix is to carry each call's outcome out of the parallel section and derive the status from all of them.
The setup: two calls, one record
I was working on a background worker that generates one record from two LLM calls. Each call produces one half of the result, the two halves are independent, so the worker starts both and awaits them together. Each call goes through a client that returns a Result type: success with a value, or failure with an error. Nothing throws on a model error, a timeout or a schema validation failure; the client turns all of them into a failed Result.
That style is a good one. It makes failure part of the method signature, so nobody forgets to handle it, and it keeps exceptions for things that really are exceptional. The trouble started one layer up, where the two Results were turned into values.
Why the half-failure looked clean
The generation method unwrapped each Result into its value and used an empty value for a failure, logging a warning. Then the worker wrote whatever came back and set the run metric:
- nothing written: Error
- anything written: Success
The case where both calls failed had already been handled, because nothing was written and the worker reported an error. The case where exactly one call failed fell straight through. One half was written, so the metric said Success. The only trace of the failure was a warning log line that nobody alerts on. The row was half fresh, half stale, and every dashboard said the run was fine.
The cause is that the empty-value mapping threw away information the caller needed. "Empty" meant both "the model had nothing to say" and "the call failed", and once those two collapse into one value, no check downstream can tell them apart.
Carry the outcome out with the values
The change is small. The generation method returns a flag alongside the two values, set when any call failed. The status is Success only when something was written and nothing failed. Here is a minimal version of the pattern, independent of any particular Result library:
public readonly record struct Result<T>(T? Value, string? Error)
{
public bool IsSuccess => Error is null;
public static Result<T> Ok(T value) => new(value, null);
public static Result<T> Fail(string error) => new(default, error);
}
public sealed record Generated(string? Summary, string? Highlights, bool AnyCallFailed);
public async Task<Generated> GenerateAsync(Input input, CancellationToken ct)
{
Task<Result<string>> summaryTask = _llm.GenerateSummaryAsync(input, ct);
Task<Result<string>> highlightsTask = _llm.GenerateHighlightsAsync(input, ct);
await Task.WhenAll(summaryTask, highlightsTask);
Result<string> summary = summaryTask.Result;
Result<string> highlights = highlightsTask.Result;
return new Generated(
summary.IsSuccess ? summary.Value : null,
highlights.IsSuccess ? highlights.Value : null,
AnyCallFailed: !summary.IsSuccess || !highlights.IsSuccess);
}
public static RunStatus StatusFor(Generated g, bool wroteSomething) =>
wroteSomething && !g.AnyCallFailed ? RunStatus.Success : RunStatus.Error;
Reading .Result after await Task.WhenAll is safe: both tasks have completed, and because the calls return failed Results rather than throwing, neither task is faulted.
I used a plain boolean rather than returning both Results to the caller. The worker doesn't need the error details, only whether to trust the run, and a flag keeps the persistence code unchanged. If you need to report which half failed, return the Results themselves instead.
Keep the half that worked
One tempting fix is to treat any failure as total and write nothing. I didn't, because the surviving half is good output: it came from a successful call and is newer than what's stored. Throwing it away would leave the whole row stale instead of half of it. So the worker still writes what succeeded, and only the status changes. The row is as fresh as it can be, and the run is honestly marked as an error so it gets looked at.
Which choice is right depends on whether the two halves have to agree with each other. If one half refers to the other, writing them separately could leave a contradictory row, and all-or-nothing is safer. Here they were independent, so partial writes were fine.
Report the failure so it groups well
The failure now also goes to error tracking, not just a warning log. One detail matters there: the message leaves out per-record identifiers such as the entity's name. Error trackers group issues by message, so a message with a name in it creates a new issue for every record. Put identifiers in structured context or tags, and keep the message itself about the shape of the failure ("one of two generation calls failed"). Then a hundred records failing the same way show up as one issue with a hundred events.
How to test it
The test that pins this is the half-failure case, because it's the one that slipped through before. Stub the LLM client so one call returns a failed Result and the other succeeds, run the worker, then assert two things:
- the successful half was persisted;
- the run metric is Error, never Success.
The both-failed and both-succeeded cases are worth keeping too, but they were already right. It's the mixed case that tells you whether status comes from outcomes or from output.
Where this pattern shows up
This isn't specific to LLM calls. Any time you fan out with Task.WhenAll over methods that return Results, the same trap is there: Task.WhenAll will never surface a failure for you, because from its point of view every task succeeded. With exceptions, Task.WhenAll at least faults the combined task when any call throws. With Results, failure is just data, and data only matters if someone reads it. LLM calls make it more likely simply because they fail more often than most dependencies: timeouts, rate limits and schema mismatches are routine.
Key takeaways
- When parallel calls return Result values,
Task.WhenAllcompletes successfully even if every call failed, so the caller must inspect each outcome. - Mapping a failed Result to an empty value loses the difference between "no output" and "the call failed", so carry a failure flag out with the values.
- Derive the run status from every call's outcome, not from whether anything was written.
- Writing the half that succeeded is fine when the halves are independent; mark the run as an error anyway.
- Keep per-record identifiers out of error-tracking messages so failures group by shape, and test the mixed case explicitly.