Skip to content

流响应接收的chunk与postman接收到的不一样 #2384

@ddxl123

Description

@ddxl123

Package

dio

Version

5.8.0+1

Operating-System

Android

Adapter

Default Dio

Output of flutter doctor -v

Flutter assets will be downloaded from https://storage.flutter-io.cn. Make sure you trust this source!
[√] Flutter (Channel stable, 3.27.1, on Microsoft Windows [版本 10.0.26100.3194], locale zh-CN)
    • Flutter version 3.27.1 on channel stable at D:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (2 months ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[√] Windows Version (Installed version of Windows is version 10 or higher)

[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at D:\AndroidSdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: D:\AndroidStudio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.3)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.12.35527.113
    • Windows 10 SDK version 10.0.22621.0

[√] Android Studio (version 2024.2)
    • Android Studio at D:\AndroidStudio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)

[√] IntelliJ IDEA Ultimate Edition (version 2023.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA 2023.3.4
    • Flutter plugin version 82.0.1
    • Dart plugin version 233.15325.11

[√] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 12 (API 31) (emulator)
    • Windows (desktop)            • windows       • windows-x64    • Microsoft Windows [版本 10.0.26100.3194]
    • Edge (web)                   • edge          • web-javascript • Microsoft Edge 133.0.3065.82

[√] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Dart Version

3.6.0

Steps to Reproduce

我使用阿里云百炼的Deepseek V3 api,使用dio进行流式接收。
dio代码:
注意 Bearer xxx 改成自己的

void test() async {
  Dio dio = Dio();
  final response = await dio.request(
    "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions",
    options: Options(
      method: "POST",
      responseType: ResponseType.stream,
      headers: {
        "Content-Type": "application/json",
        "Authorization": "Bearer xxx", // 改成自己的
      },
    ),
    data: {
      "stream": true,
      "model": "deepseek-v3",
      "messages": [
        {"role": "user", "content": "您好"}
      ],
    },
  );
  final stream = (response.data as ResponseBody).stream;

  await for (List<int> chunk in stream) {
    String chunkString = utf8.decode(chunk);
    print("---chunk start---");
    print(chunkString);
    print("---end---");
  }
}


输出形式:

I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"","role":"assistant"},"index":0,"logprobs":null,"finish_reason":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): data: {"choices":[{"finish_reason":null,"delta":{"content":"您好"},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"!"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"很高兴"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"见到您。有什么"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"我可以帮助您的吗"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"?无论是关于某个"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"话题的疑问,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"还是需要建议,"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"我都在这里为您"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"delta":{"content":"提供帮助。😊"},"finish_reason":null,"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): ---end---
I/flutter (11467): ---chunk start---
I/flutter (11467): data: {"choices":[{"finish_reason":"stop","delta":{"content":""},"index":0,"logprobs":null}],"object":"chat.completion.chunk","usage":null,"created":1740670403,"system_fingerprint":null,"model":"deepseek-v3","id":"chatcmpl-53618005-a643-936a-ade6-aa56232c2468"}
I/flutter (11467): 
I/flutter (11467): data: [DONE]
I/flutter (11467): 
I/flutter (11467): ---end---

Expected Result

以postman为准

Actual Result

两个问题:

  1. 可以发现第一个chuck接收的数据是两个“data:”,而在postman中的这个chuck是正常分成两个chuck的,不知道到底是不是dio的bug。
    2.如何才能直接接收chuck内数据,而不带“data:”前缀?

附图:
dio

Image

postman
Image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions