From 76d8dc20e5776c2feffc2542e7bb1ad18fb548b9 Mon Sep 17 00:00:00 2001 From: Moises Tohias <48728934+moisestohias@users.noreply.github.com> Date: Mon, 12 Jan 2026 20:53:45 +0100 Subject: [PATCH] Fix Qwen provider implementation (#3322) * Fix Qwen provider implementation - Re-enable cookie generation for authentication - Update User-Agent header to latest version - Fix Authorization header to only be added when token is present - Add X-Requested-With and X-Source headers for proper API communication - Force stream and incremental_output to True for consistent streaming behavior - Set parent_id to None to prevent parent-child relationship issues - Add conditional thinking functionality with proper feature_config when enable_thinking is True - Improve header management for better compatibility with Qwen API Co-authored-by: Qwen-Coder * Restore parent_id in Qwen provider for conversation support --------- Co-authored-by: Qwen-Coder --- g4f/Provider/Qwen.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/g4f/Provider/Qwen.py b/g4f/Provider/Qwen.py index be58d793..89dbd090 100644 --- a/g4f/Provider/Qwen.py +++ b/g4f/Provider/Qwen.py @@ -336,7 +336,7 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin): prompt = get_last_user_message(messages) timeout = kwargs.get("timeout") or 5 * 60 # for _ in range(2): - # data = generate_cookies() + data = generate_cookies() # args,ua = await cls.get_args(proxy, **kwargs) headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36', @@ -349,10 +349,12 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin): 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'Connection': 'keep-alive', - # 'Cookie': f'ssxmod_itna={data["ssxmod_itna"]};ssxmod_itna2={data["ssxmod_itna2"]}', - 'Authorization': f'Bearer {token}' if token else "Bearer", - 'Source': 'web' + 'X-Requested-With': 'XMLHttpRequest', + 'Cookie': f'ssxmod_itna={data["ssxmod_itna"]};ssxmod_itna2={data["ssxmod_itna2"]}', + 'X-Source': 'web' } + if token: + headers['Authorization'] = f'Bearer {token}' # try: async with StreamSession(headers=headers) as session: @@ -435,16 +437,16 @@ class Qwen(AsyncGeneratorProvider, ProviderModelMixin): "output_schema": "phase", "thinking_budget": 81920 }, - "extra": { - "meta": { - "subChatType": chat_type - } - }, - "sub_chat_type": chat_type, - "parent_id": None + "sub_chat_type": chat_type } ] } + if enable_thinking: + msg_payload["messages"][0]["feature_config"] = { + "thinking_enabled": True, + "output_schema": "phase", + "thinking_budget": 81920 + } if aspect_ratio: msg_payload["size"] = aspect_ratio