From 83d725d2d84ee7fe033532b3951cbb214945ac64 Mon Sep 17 00:00:00 2001 From: Debanjum Date: Tue, 19 Aug 2025 23:26:45 -0700 Subject: [PATCH] Extract thoughts of openai style models like gpt-oss from api response They use delta.reasoning instead of delta.reasoning_content to share model reasoning --- src/khoj/processor/conversation/openai/utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/khoj/processor/conversation/openai/utils.py b/src/khoj/processor/conversation/openai/utils.py index 7db03f52..1d0ecdab 100644 --- a/src/khoj/processor/conversation/openai/utils.py +++ b/src/khoj/processor/conversation/openai/utils.py @@ -173,6 +173,13 @@ def completion_with_backoff( and chunk.chunk.choices[0].delta.reasoning_content ): thoughts += chunk.chunk.choices[0].delta.reasoning_content + elif ( + chunk.type == "chunk" + and chunk.chunk.choices + and hasattr(chunk.chunk.choices[0].delta, "reasoning") + and chunk.chunk.choices[0].delta.reasoning + ): + thoughts += chunk.chunk.choices[0].delta.reasoning elif chunk.type == "chunk" and chunk.chunk.choices and chunk.chunk.choices[0].delta.tool_calls: tool_ids += [tool_call.id for tool_call in chunk.chunk.choices[0].delta.tool_calls] elif chunk.type == "tool_calls.function.arguments.done": @@ -945,6 +952,14 @@ async def astream_thought_processor( ): tchunk.choices[0].delta.thought = chunk.choices[0].delta.reasoning_content + # Handlle openai reasoning style response with thoughts. Used by gpt-oss. + if ( + len(tchunk.choices) > 0 + and hasattr(tchunk.choices[0].delta, "reasoning") + and tchunk.choices[0].delta.reasoning + ): + tchunk.choices[0].delta.thought = chunk.choices[0].delta.reasoning + # Handlle llama.cpp server style response with thoughts. elif len(tchunk.choices) > 0 and tchunk.choices[0].delta.model_extra.get("reasoning_content"): tchunk.choices[0].delta.thought = tchunk.choices[0].delta.model_extra.get("reasoning_content")