Skip to content

Conversation

@kaspermarstal
Copy link
Contributor

Add comprehensive unit tests for MistralThinkingBehavior and Mistral provider to reproduce and verify issue #309 ("Mistral provider broken" with "Object reference not set to an instance of an object" error).

Tests cover:

  • MistralThinkingBehavior with null/empty text in assistant messages
  • Thinking response extraction from JSON arrays
  • Edge cases with malformed JSON responses
  • ChatMessage and ChatResponse handling with null text
  • Mocked IChatClient scenarios simulating Mistral SDK behavior
  • Exact pattern used in CellmFunctions.cs line 317

Add comprehensive unit tests for MistralThinkingBehavior and Mistral
provider to reproduce and verify issue #309 ("Mistral provider broken"
with "Object reference not set to an instance of an object" error).

Tests cover:
- MistralThinkingBehavior with null/empty text in assistant messages
- Thinking response extraction from JSON arrays
- Edge cases with malformed JSON responses
- ChatMessage and ChatResponse handling with null text
- Mocked IChatClient scenarios simulating Mistral SDK behavior
- Exact pattern used in CellmFunctions.cs line 317
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@kaspermarstal kaspermarstal changed the title Reproduce issue 309 from cellm repository fix: Mistral "Object reference not set to an intance of object" Jan 7, 2026
claude added 2 commits January 7, 2026 09:10
ROOT CAUSE IDENTIFIED:
The bug is in Mistral.SDK v2.3.0's CompletionsEndpoint.ChatClient.cs
in the ProcessResponseContent method:

```csharp
foreach (var content in response.Choices)
{
    if (content.Message.ToolCalls is not null)  // BUG: No null check!
```

When the Mistral API returns a response where Choice.Message is null
(which can happen in edge cases), accessing content.Message.ToolCalls
throws NullReferenceException: "Object reference not set to an
instance of an object"

COMPARISON:
- Streaming code (CORRECT): choice.Delta?.ToolCalls (null-conditional)
- Non-streaming (BUG): content.Message.ToolCalls (no null check)

The tests simulate the exact JSON response that triggers the bug
by creating a response without a message field, which deserializes
to null.

FIX: The SDK should use content.Message?.ToolCalls instead.
- Add #pragma warning disable for intentional null dereference in bug reproduction test
- Rename async method to use 'Async' suffix per naming conventions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants