Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when retrieving properties of a streaming chat chunk using Asynchronous Filter #198

Open
2 tasks done
RogerBarreto opened this issue Aug 29, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@RogerBarreto
Copy link
Contributor

RogerBarreto commented Aug 29, 2024

Confirm this is not an issue with the underlying OpenAI API

  • This is an issue with the Python library

Confirm this is not an issue with Azure OpenAI

  • This is not an issue with Azure OpenAI

Describe the bug

When using StreamingChatCompletionUpdate with Asynchronous Filter enabled in azure, this class breaks.

When a SSE data: doesn't have a delta (null) the Choice[0].Delta line throws NullReferenceException.

Final 3 streaming chunks of an Asynchornous Filter response.

data: {"choices":[{"delta":{},"finish_reason":"stop","index":0,"logprobs":null}],"created":1724860848,"id":"chatcmpl-A1FOCU4vamBSxxqjaKcvADYMlwlaC","model":"gpt-4o-2024-05-13","object":"chat.completion.chunk","system_fingerprint":"fp_abc28019ad"}

data: {"choices":[{"content_filter_offsets":{"check_offset":1576,"start_offset":1576,"end_offset":2318},"content_filter_results":{"hate":{"filtered":false,"severity":"safe"},"self_harm":{"filtered":false,"severity":"safe"},"sexual":{"filtered":false,"severity":"safe"},"violence":{"filtered":false,"severity":"safe"}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}

data: {"choices":[{"content_filter_offsets":{"check_offset":1576,"start_offset":1576,"end_offset":2318},"content_filter_results":{"protected_material_code":{"filtered":false,"detected":false},"protected_material_text":{"filtered":false,"detected":false}},"finish_reason":null,"index":0}],"created":0,"id":"","model":"","object":""}

This issue was reported in our Semantic Kernel Issues Here but actually seems to be a problem when Azure tries to use the OpenAI SDK for those chunks, this potentially can be handled by Azure SDK library in a non-ideal (try catch block) or in the back-end (providing always an empty delta) but I wonder would be quicker/simple getting a fix in here.

The problem also happen for other properties of the class as below:

image

To Reproduce

  1. Enable Asynchronous Filter in Azure or OpenAI (if possible)
  2. Make a streaming request and try to check the Role property of the last 2 chunks

Since the property is a pointer, we are unable to test it for null.

if (lastChunk.Role.HasValue) { } // throws
if (lastChunk.Role is null) { } // throws

Code snippets

No response

OS

Windows 11

.NET version

.net 8

Library version

beta.10

@RogerBarreto RogerBarreto added the bug Something isn't working label Aug 29, 2024
@RogerBarreto RogerBarreto changed the title NullPointException when retrieving the role of a streaming chat chunk using Asynchronous Filter NullReferenceException when retrieving the role of a streaming chat chunk using Asynchronous Filter Aug 30, 2024
@RogerBarreto RogerBarreto changed the title NullReferenceException when retrieving the role of a streaming chat chunk using Asynchronous Filter NullReferenceException when retrieving properties of a streaming chat chunk using Asynchronous Filter Aug 30, 2024
RogerBarreto added a commit to microsoft/semantic-kernel that referenced this issue Aug 30, 2024
### Motivation and Context

Resolve #8407
 
### Description

Add temporary bugfix for error described in below issues:

- #8407
- openai/openai-dotnet#198
- Added UnitTest to ensure expected behavior.

### Contribution Checklist

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone 😄
@sandrohanea
Copy link

The problem seems to be that the SDK assumes that first "Choices" object always have a not-null Delta, while for content_filter_offsets this is not true.

https://github.com/openai/openai-dotnet/blob/dce79fc17963d0550f790db7158cbb0df7c186ec/src/Custom/Chat/StreamingChatCompletionUpdate.cs#L110C11-L110C27

Propagating null should help to not throw in this case: Choices[0].Delta.FunctionCall => Choices[0].Delta?.FunctionCall

github-merge-queue bot pushed a commit to microsoft/semantic-kernel that referenced this issue Sep 10, 2024
…tion when using Function Calling. (#8654)

### Motivation and Context

Closes #8629 

Workaround BugFix for issue in OpenAI SDK:

- openai/openai-dotnet#198
github-merge-queue bot pushed a commit to microsoft/semantic-kernel that referenced this issue Sep 10, 2024
…tion when using Function Calling. (#8654)

### Motivation and Context

Closes #8629 

Workaround BugFix for issue in OpenAI SDK:

- openai/openai-dotnet#198
@joseharriaga
Copy link
Collaborator

Thank you for reaching out, @RogerBarreto ! We have merged and released a fix for this issue. It is available starting with version 2.0.0-beta.12.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants