Skip to content

Enhance DeepSeek streaming, uploads, and testing support - #3507

Open
3mora2 wants to merge 6 commits into
xtekky:mainfrom
3mora2:fix
Open

Enhance DeepSeek streaming, uploads, and testing support#3507
3mora2 wants to merge 6 commits into
xtekky:mainfrom
3mora2:fix

Conversation

@3mora2

@3mora2 3mora2 commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR modernizes the authenticated DeepSeek provider to support the current web chat protocol, including reliable SSE streaming, proof-of-work challenges, file uploads, stream recovery, and comprehensive tests.

What changed

Streaming and response recovery

  • Added a dedicated SSE parser that preserves DeepSeek event types and supports snapshots, patches, batches, and close events.
  • Added support for continuing INCOMPLETE responses and resuming interrupted streams.
  • Tracks fragment types and indexes, including indexed SET, APPEND, /type, /content, and -1 patches.
  • Emits THINK, search, and tool fragments as reasoning while keeping RESPONSE and TEMPLATE_RESPONSE as visible output.
  • Prevents duplicate content when a stream is resumed or replaced by a full-message snapshot.
  • Attempts one bounded recovery when DeepSeek closes with FINISHED but provides no visible response.
  • Raises a clear ResponseError if the recovered message still contains no response instead of silently returning an empty result.
  • Never promotes hidden reasoning content to the final response.

File uploads and proof of work

  • Added endpoint-specific DeepSeek PoW challenge generation and solving.
  • Runs the CPU-intensive PoW solver outside the async event loop.
  • Added multipart file uploads with MIME type and filename handling.
  • Supports multiple uploaded files while preserving their order.
  • Waits for server-side file parsing and handles terminal parsing failures explicitly.
  • Updated detect_file_type to consistently return the detected extension and MIME type.

Provider structure and protocol handling

  • Moved PoW and streaming logic into dedicated deepseek helper modules.
  • Added normalized chat and upload headers.
  • Added explicit completion payload construction and business-response envelope validation.
  • Updated chat session creation, deletion, upload, completion, continue, and resume handling to match the observed DeepSeek web contract.
  • Improved diagnostic logging without exposing response content or authentication data.

Testing and diagnostics

  • Added unit coverage for SSE parsing, fragment classification, status handling, resume and continue flows, empty responses, PoW, uploads, file parsing, and error responses.
  • Added a reusable JSONL chunk logger for inspecting provider metadata and reasoning chunks during development.

Why

DeepSeek streams responses as incremental fragment patches and can occasionally close a message as FINISHED after emitting reasoning without a final response. Previously, this could result in a successful request with an empty visible answer.

The updated implementation recovers the complete message when possible and returns an explicit error when DeepSeek genuinely provides no final response.

Validation

  • python -m unittest etc.unittest.test_deepseek_stream etc.unittest.test_deepseek_upload
    • 40 tests, OK
  • python -m etc.unittest
    • 235 tests run
    • 9 skipped
    • Suite OK

Final verification was performed locally without issuing an additional live DeepSeek request.

3mora2 added 6 commits August 26, 2026 23:02
Add full DeepSeek provider streaming and file upload support plus unit tests. Implement SSE parser (iter_deepseek_sse), robust stream state handling, resume/continue logic (iter_chat_stream), PoW challenge solving (create_pow_response), multipart uploads (upload_file) and poll-based parsing wait (wait_for_file_parsed). Improve header handling (_build_chat_headers/_build_upload_session_headers), fragment/status processing and diagnostic logging. Add unit tests covering chunk logging, streaming and upload flows (etc/unittest/test_deepseek_*.py). Also adjust image.detect_file_type signature to always return (extension, mime).
Move DeepSeek PoW and SSE stream logic into g4f/Provider/needs_auth/deepseek (pow.py, stream.py) and add small init. Refactor DeepSeek.py to import and use these helpers, add _unwrap_biz_response, _build_completion_payload, _resolve_upload_metadata, and upload_files. Simplify delete_chat_session to the observed POST contract and return bool. Improve header/timezone handling, error checking for business envelope, file parsing failure statuses, and run PoW off the event loop (asyncio.to_thread). Update and extend unit tests to cover new behaviors.
Add ChunkJsonlWriter and helpers in etc/testing/deepseek_chunk_log.py to record provider response chunks to a durable JSONL journal (with reader and store_or_collect_chunk). Update unit test import in etc/unittest/test_deepseek_chunk_log.py to use the new helper path. Also remove stray trailing blank lines in deepseek package files (pow.py, stream.py, __init__.py).
Enhance DeepSeek streaming: track fragment kinds and indices, properly handle indexed SET/APPEND and type/content patches, and emit reasoning/response chunks based on fragment types. Add _record_fragment_kind and _fragment_index_from_path, plus new state fields (fragment_kinds, next_fragment_index). Improve resume logic in DeepSeek provider to attempt a single resume for FINISHED streams with no response, track empty_response_resume_attempted, and raise ResponseError when appropriate. Update and extend unit tests to cover fragment indexing, resumed full messages, and empty-resume behaviors.
This change updates the DeepSeek upload flow to solve PoW challenges using the running event loop's executor instead of asyncio.to_thread. It keeps the async behavior consistent with the test environment and avoids the underlying issue seen in the upload path. The unit test was also updated to assert the executor call pattern and validate the expected PoW response handling.
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.

1 participant