@@ -559,24 +559,7 @@ def _process_response(self, response: chat.ChatCompletion | str) -> ModelRespons
559559 # - https://openrouter.ai/docs/use-cases/reasoning-tokens#preserving-reasoning-blocks
560560 # If you need this, please file an issue.
561561
562- vendor_details : dict [str , Any ] = {}
563-
564- # Add logprobs to vendor_details if available
565- if choice .logprobs is not None and choice .logprobs .content :
566- # Convert logprobs to a serializable format
567- vendor_details ['logprobs' ] = [
568- {
569- 'token' : lp .token ,
570- 'bytes' : lp .bytes ,
571- 'logprob' : lp .logprob ,
572- 'top_logprobs' : [
573- {'token' : tlp .token , 'bytes' : tlp .bytes , 'logprob' : tlp .logprob } for tlp in lp .top_logprobs
574- ],
575- }
576- for lp in choice .logprobs .content
577- ]
578-
579- if choice .message .content is not None :
562+ if choice .message .content :
580563 items .extend (
581564 (replace (part , id = 'content' , provider_name = self .system ) if isinstance (part , ThinkingPart ) else part )
582565 for part in split_content_into_text_and_thinking (choice .message .content , self .profile .thinking_tags )
@@ -594,6 +577,23 @@ def _process_response(self, response: chat.ChatCompletion | str) -> ModelRespons
594577 part .tool_call_id = _guard_tool_call_id (part )
595578 items .append (part )
596579
580+ vendor_details : dict [str , Any ] = {}
581+
582+ # Add logprobs to vendor_details if available
583+ if choice .logprobs is not None and choice .logprobs .content :
584+ # Convert logprobs to a serializable format
585+ vendor_details ['logprobs' ] = [
586+ {
587+ 'token' : lp .token ,
588+ 'bytes' : lp .bytes ,
589+ 'logprob' : lp .logprob ,
590+ 'top_logprobs' : [
591+ {'token' : tlp .token , 'bytes' : tlp .bytes , 'logprob' : tlp .logprob } for tlp in lp .top_logprobs
592+ ],
593+ }
594+ for lp in choice .logprobs .content
595+ ]
596+
597597 raw_finish_reason = choice .finish_reason
598598 vendor_details ['finish_reason' ] = raw_finish_reason
599599 finish_reason = _CHAT_FINISH_REASON_MAP .get (raw_finish_reason )
@@ -1616,21 +1616,6 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
16161616 self .provider_details = {'finish_reason' : raw_finish_reason }
16171617 self .finish_reason = _CHAT_FINISH_REASON_MAP .get (raw_finish_reason )
16181618
1619- # Handle the text part of the response
1620- content = choice .delta .content
1621- if content is not None :
1622- maybe_event = self ._parts_manager .handle_text_delta (
1623- vendor_part_id = 'content' ,
1624- content = content ,
1625- thinking_tags = self ._model_profile .thinking_tags ,
1626- ignore_leading_whitespace = self ._model_profile .ignore_streamed_leading_whitespace ,
1627- )
1628- if maybe_event is not None : # pragma: no branch
1629- if isinstance (maybe_event , PartStartEvent ) and isinstance (maybe_event .part , ThinkingPart ):
1630- maybe_event .part .id = 'content'
1631- maybe_event .part .provider_name = self .provider_name
1632- yield maybe_event
1633-
16341619 # The `reasoning_content` field is only present in DeepSeek models.
16351620 # https://api-docs.deepseek.com/guides/reasoning_model
16361621 if reasoning_content := getattr (choice .delta , 'reasoning_content' , None ):
@@ -1652,6 +1637,21 @@ async def _get_event_iterator(self) -> AsyncIterator[ModelResponseStreamEvent]:
16521637 provider_name = self .provider_name ,
16531638 )
16541639
1640+ # Handle the text part of the response
1641+ content = choice .delta .content
1642+ if content :
1643+ maybe_event = self ._parts_manager .handle_text_delta (
1644+ vendor_part_id = 'content' ,
1645+ content = content ,
1646+ thinking_tags = self ._model_profile .thinking_tags ,
1647+ ignore_leading_whitespace = self ._model_profile .ignore_streamed_leading_whitespace ,
1648+ )
1649+ if maybe_event is not None : # pragma: no branch
1650+ if isinstance (maybe_event , PartStartEvent ) and isinstance (maybe_event .part , ThinkingPart ):
1651+ maybe_event .part .id = 'content'
1652+ maybe_event .part .provider_name = self .provider_name
1653+ yield maybe_event
1654+
16551655 for dtc in choice .delta .tool_calls or []:
16561656 maybe_event = self ._parts_manager .handle_tool_call_delta (
16571657 vendor_part_id = dtc .index ,
0 commit comments