Enhance DeepSeek streaming, uploads, and testing support - #3507
Open
3mora2 wants to merge 6 commits into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
INCOMPLETEresponses and resuming interrupted streams.SET,APPEND,/type,/content, and-1patches.THINK, search, and tool fragments as reasoning while keepingRESPONSEandTEMPLATE_RESPONSEas visible output.FINISHEDbut provides no visible response.ResponseErrorif the recovered message still contains no response instead of silently returning an empty result.File uploads and proof of work
detect_file_typeto consistently return the detected extension and MIME type.Provider structure and protocol handling
deepseekhelper modules.Testing and diagnostics
Why
DeepSeek streams responses as incremental fragment patches and can occasionally close a message as
FINISHEDafter 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_uploadpython -m etc.unittestFinal verification was performed locally without issuing an additional live DeepSeek request.