Skip to content

REST web server returns "500 Internal Server Error" serializing error response #543

@f2bo

Description

@f2bo

I notice that the REST web server always returns '500 Internal Server Error' whenever there's an error in a request regardless of the type of error. For client errors I would expect a '400 Bad Request' instead. Looking at the logs it seems that there's an incorrect configuration of the TypeInfoResolver used to serialize the response.

Two examples:

- Request specifies a model that is not loaded

2026-03-22 11:07:50.339 -03:00 [INF] Model loaded successfully: qwen2.5-0.5b-instruct-generic-gpu:4
2026-03-22 11:07:50.339 -03:00 [INF] [Telemetry] AppName:foundry_local_samples UserAgent:CoreNativeInterop Command:ModelLoad Status:Success Direct:True Time:2803ms
2026-03-22 11:08:48.180 -03:00 [INF] Request starting HTTP/1.1 POST http://127.0.0.1:8651/v1/chat/completions - application/json 465
2026-03-22 11:08:48.181 -03:00 [INF] Executing endpoint 'HTTP: POST /v1/chat/completions => ChatCompletionsAsync'
2026-03-22 11:08:48.182 -03:00 [INF] HandleChatCompletionAsStreamRequest -> model:qwen2.5-0.5b-instruct-generic-cpu:4 MaxCompletionTokens:null maxTokens:100 temperature:0.7 topP:1
2026-03-22 11:08:48.185 -03:00 [ERR] Failed to handle OpenAI completion
System.InvalidOperationException: Model 'qwen2.5-0.5b-instruct-generic-cpu:4' is not loaded. Please load the model before getting a ChatClient.
   at Microsoft.AI.Foundry.Local.FoundryLocalCore.<GetChatClientAsync>d__35.MoveNext() + 0x351
--- End of stack trace from previous location ---
   at Microsoft.AI.Foundry.Local.Service.OpenAIApiProvider.<HandleChatCompletionRequestAsync>d__6.MoveNext() + 0x6c
--- End of stack trace from previous location ---
   at Microsoft.AI.Foundry.Local.Service.OpenAIApi.<ChatCompletionsAsync>d__1.MoveNext() + 0x3ab
2026-03-22 11:08:48.185 -03:00 [INF] Setting HTTP status code 500.
2026-03-22 11:08:48.185 -03:00 [INF] Executed endpoint 'HTTP: POST /v1/chat/completions => ChatCompletionsAsync'
2026-03-22 11:08:48.185 -03:00 [ERR] Connection id "0HNK83I4P723U", Request id "0HNK83I4P723U:00000001": An unhandled exception was thrown by the application.
System.NotSupportedException: JsonTypeInfo metadata for type 'Microsoft.AspNetCore.Mvc.ProblemDetails' was not provided by TypeInfoResolver of type '[Microsoft.AI.Foundry.Local.JsonSerializationContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type, IJsonTypeInfoResolver) + 0x61
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x9e
   at System.Text.Json.JsonSerializerOptions.GetTypeInfo(Type) + 0x4e
   at Microsoft.AspNetCore.Http.HttpResultsHelper.WriteResultAsJsonAsync[TValue](HttpContext, ILogger, TValue, String, JsonSerializerOptions) + 0x59
   at Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult.<ExecuteAsync>d__14.MoveNext() + 0x3d1
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FBF6DABC0CAE83DDA6B503B3D38E39809969A45212DD5FEC17DC9F34EA8FE57C0__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass6_0.<<MapPost3>g__RequestHandler|5>d.MoveNext() + 0x110
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<<Invoke>g__AwaitRequestTask|7_0>d.MoveNext() + 0x5a
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequests>d__238`1.MoveNext() + 0x2f2
2026-03-22 11:08:48.186 -03:00 [INF] Request finished HTTP/1.1 POST http://127.0.0.1:8651/v1/chat/completions - 500 0 null 5.643ms

- Request specifies invalid content type

2026-03-22 11:13:27.124 -03:00 [INF] Request starting HTTP/1.1 POST http://127.0.0.1:8651/v1/chat/completions - application/x-www-form-urlencoded 82
2026-03-22 11:13:27.124 -03:00 [INF] Executing endpoint 'HTTP: POST /v1/chat/completions => ChatCompletionsAsync'
2026-03-22 11:13:27.124 -03:00 [ERR] Invalid OpenAI JSON request
System.InvalidOperationException: Unable to read the request as JSON because the request content type 'application/x-www-form-urlencoded' is not a known JSON content type.
   at Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.ThrowContentTypeError(HttpRequest) + 0x4b
   at Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.<ReadFromJsonAsync>d__4`1.MoveNext() + 0x410
--- End of stack trace from previous location ---
   at Microsoft.AI.Foundry.Local.Service.OpenAIApi.<ChatCompletionsAsync>d__1.MoveNext() + 0x21d
2026-03-22 11:13:27.124 -03:00 [INF] Setting HTTP status code 500.
2026-03-22 11:13:27.124 -03:00 [INF] Executed endpoint 'HTTP: POST /v1/chat/completions => ChatCompletionsAsync'
2026-03-22 11:13:27.124 -03:00 [ERR] Connection id "0HNK83I4P7240", Request id "0HNK83I4P7240:00000001": An unhandled exception was thrown by the application.
System.NotSupportedException: JsonTypeInfo metadata for type 'Microsoft.AspNetCore.Mvc.ProblemDetails' was not provided by TypeInfoResolver of type '[Microsoft.AI.Foundry.Local.JsonSerializationContext]'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type, IJsonTypeInfoResolver) + 0x61
   at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x9e
   at System.Text.Json.JsonSerializerOptions.GetTypeInfo(Type) + 0x4e
   at Microsoft.AspNetCore.Http.HttpResultsHelper.WriteResultAsJsonAsync[TValue](HttpContext, ILogger, TValue, String, JsonSerializerOptions) + 0x59
   at Microsoft.AspNetCore.Http.HttpResults.ProblemHttpResult.<ExecuteAsync>d__14.MoveNext() + 0x3d1
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.Generated.<GeneratedRouteBuilderExtensions_g>FBF6DABC0CAE83DDA6B503B3D38E39809969A45212DD5FEC17DC9F34EA8FE57C0__GeneratedRouteBuilderExtensionsCore.<>c__DisplayClass6_0.<<MapPost3>g__RequestHandler|5>d.MoveNext() + 0x110
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<<Invoke>g__AwaitRequestTask|7_0>d.MoveNext() + 0x5a
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.<ProcessRequests>d__238`1.MoveNext() + 0x2f2
2026-03-22 11:13:27.124 -03:00 [INF] Request finished HTTP/1.1 POST http://127.0.0.1:8651/v1/chat/completions - 500 0 null 0.3935ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions