Skip to content

OllamaChatClient doesn't check for status code before deserializing. #5820

@eiriktsarpalis

Description

@eiriktsarpalis

Description

Per the title, the client currently doesn't check response status codes before deserializing in both the non-streaming

var response = (await httpResponse.Content.ReadFromJsonAsync(
JsonContext.Default.OllamaChatResponse,
cancellationToken).ConfigureAwait(false))!;
if (!string.IsNullOrEmpty(response.Error))
{
throw new InvalidOperationException($"Ollama error: {response.Error}");
}

and streaming implementations

using var httpResponse = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
using var httpResponseStream = await httpResponse.Content
#if NET
.ReadAsStreamAsync(cancellationToken)
#else
.ReadAsStreamAsync()
#endif
.ConfigureAwait(false);

In practice, this results in one single empty chat update being returned in cases where, for example, Ollama returns a 404 response. We should fix this and ensure that OllamaSharp doesn't suffer from the same issue.

Reproduction Steps

N/A

Expected behavior

Should throw an exception when non 20x responses are being produced.

Actual behavior

Does not throw an exception when non-20x responses are being produced.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions