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

Chat Stream Hangs for 15 Seconds After Completion #206

Open
geomags3 opened this issue Jan 29, 2025 · 0 comments
Open

Chat Stream Hangs for 15 Seconds After Completion #206

geomags3 opened this issue Jan 29, 2025 · 0 comments

Comments

@geomags3
Copy link

When using OpenAI.instance.chat.createStream to create a chat completion stream, the stream remains open for ~15 seconds after completion, even after calling subscription.cancel(). The delay disappears when the subscription is cancelled manually after a short delay.

Steps to Reproduce

  1. Create a chat stream using OpenAI.instance.chat.createStream().
  2. Subscribe to the stream and print responses.
  3. Manually cancel the subscription after a short delay (e.g., 1 second).
  4. Observe that without manual cancellation, the process hangs for ~15 seconds.

Expected Behavior

  • When the stream completes, it should close immediately.
  • Calling subscription.cancel() should immediately stop any lingering connections.

Actual Behavior

  • After "Stream completed" and "Subscription cancelled", the process hangs for ~15 seconds before stopping.
  • When subscription.cancel() is delayed by 1 second, the process stops immediately.

Example Code

🛑 Problem: 15-Second Delay Without Manual Cancellation

import 'dart:async';
import 'package:dart_openai/dart_openai.dart';

void main() async {
  OpenAI.apiKey = 'your-api-key';

  Stream stream = OpenAI.instance.chat.createStream(
    model: 'gpt-4o-mini',
    messages: [
      OpenAIChatCompletionChoiceMessageModel(
        content: [OpenAIChatCompletionChoiceMessageContentItemModel.text('Tell me 10 jokes.')],
        role: OpenAIChatMessageRole.user,
      ),
    ],
  );

  late StreamSubscription sub;
  sub = stream.listen(
    (res) => print(res.choices),
    onDone: () async {
      print('✅ Stream completed');
      await sub.cancel();
      print('✅ Subscription cancelled');
    },
    onError: (e) => print('❌ Stream error: $e'),
  );

  // ❌ No manual cancellation
}

⏳ Observed Behavior:

[Assistant Response: Joke 1]
...
[Assistant Response: Joke 10]
✅ Stream completed
✅ Subscription cancelled
(Hangs for 15 seconds)

✅ Immediate Termination with Manual Delay + sub.cancel()

import 'dart:async';
import 'package:dart_openai/dart_openai.dart';

void main() async {
  OpenAI.apiKey = 'your-api-key';

  Stream stream = OpenAI.instance.chat.createStream(
    model: 'gpt-4o-mini',
    messages: [
      OpenAIChatCompletionChoiceMessageModel(
        content: [OpenAIChatCompletionChoiceMessageContentItemModel.text('Tell me 10 jokes.')],
        role: OpenAIChatMessageRole.user,
      ),
    ],
  );

  late StreamSubscription sub;
  sub = stream.listen(
    (res) => print(res.choices),
    onDone: () async {
      print('✅ Stream completed');
      await sub.cancel();
      print('✅ Subscription cancelled');
    },
    onError: (e) => print('❌ Stream error: $e'),
  );

  // ✅ Manual delayed cancellation stops the process immediately
  await Future.delayed(const Duration(seconds: 1));
  await sub.cancel();
  print('✅ Manually cancelled subscription after delay');
}

🚀 Observed Behavior:

[Assistant Response: Joke 1]
...
[Assistant Response: Joke 10]
✅ Stream completed
✅ Subscription cancelled
✅ Manually cancelled subscription after delay
(Process stops immediately)

Would appreciate any insights or workarounds! Thanks! 🙌

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

No branches or pull requests

1 participant