Skip to content

Commit 9caf768

Browse files
committed
gptel-openai: Simplify reasoning handling further
* gptel-openai.el (gptel-curl--parse-stream): Simplify the reasoning handling for OpenAI-compatible APIs (#901). It now works as follows: - Look for :reasoning content. If found, capture it and do nothing else. - Otherwise, check if this is a reasoning model. - If it is, check if we are receiving non-empty content. - If we are, signal the end of the reasoning block. The last step sets :reasoning-block to t. The process filter handles closing out the reasoning handling and setting it to done.
1 parent 9d32489 commit 9caf768

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

gptel-openai.el

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,15 @@ information if the stream contains it."
232232
(push (plist-get func :arguments) (plist-get info :partial_json)))))
233233
;; Check for reasoning blocks, currently only used by Openrouter
234234
(unless (eq (plist-get info :reasoning-block) 'done)
235-
;; We could also check for (plist-member delta :reasoning)
236-
;; here, but some APIs don't include it with content. (#901)
237-
(if-let* ((reasoning-chunk (plist-get delta :reasoning)) ;for openrouter
235+
(if-let* ((reasoning-chunk (plist-get delta :reasoning)) ;for Openrouter and co
238236
((not (eq reasoning-chunk :null))))
239237
(plist-put info :reasoning
240238
(concat (plist-get info :reasoning) reasoning-chunk))
241239
;; Done with reasoning if we get non-empty content
242-
(if-let* ((c (plist-get delta :content))
240+
(if-let* (((plist-member info :reasoning)) ;Is this a reasoning model?
241+
(c (plist-get delta :content)) ;Started receiving text content?
243242
((not (or (eq c :null) (string-empty-p c)))))
244-
(if (plist-member info :reasoning) ;Is this a reasoning model?
245-
(plist-put info :reasoning-block t) ;End of streaming reasoning block
246-
(plist-put info :reasoning-block 'done))))))))) ;Not using a reasoning model
243+
(plist-put info :reasoning-block t)))))))) ;Signal end of reasoning block
247244
(error (goto-char (match-beginning 0))))
248245
(apply #'concat (nreverse content-strs))))
249246

0 commit comments

Comments
 (0)