1+ import re
12from dataclasses import asdict , dataclass
23from typing import Any , Literal , cast
34
@@ -286,7 +287,8 @@ class OpenRouterThinkingPart(ThinkingPart):
286287 __repr__ = _utils .dataclasses_no_defaults_repr
287288
288289 @classmethod
289- def from_reasoning_detail (cls , reasoning : OpenRouterReasoningDetail , provider_name : str ):
290+ def from_reasoning_detail (cls , reasoning : OpenRouterReasoningDetail ):
291+ provider_name = 'openrouter'
290292 if isinstance (reasoning , ReasoningText ):
291293 return cls (
292294 id = reasoning .id ,
@@ -447,8 +449,7 @@ def _process_response(self, response: ChatCompletion | str) -> ModelResponse:
447449
448450 if reasoning_details := choice .message .reasoning_details :
449451 new_parts : list [ThinkingPart ] = [
450- OpenRouterThinkingPart .from_reasoning_detail (reasoning , native_response .provider )
451- for reasoning in reasoning_details
452+ OpenRouterThinkingPart .from_reasoning_detail (reasoning ) for reasoning in reasoning_details
452453 ]
453454
454455 model_response .parts = [* new_parts , * model_response .parts ]
@@ -464,8 +465,15 @@ async def _map_messages(self, messages: list[ModelMessage]) -> list[ChatCompleti
464465 for message , openai_message in zip (messages , openai_messages ):
465466 if isinstance (message , ModelResponse ):
466467 if reasoning_details := [
467- part .into_reasoning_detail () for part in message .parts if isinstance (part , OpenRouterThinkingPart )
468+ part .into_reasoning_detail ()
469+ for part in message .parts
470+ if isinstance (part , OpenRouterThinkingPart ) and part .provider_name == self .system
468471 ]:
469472 openai_message ['reasoning_details' ] = reasoning_details # type: ignore[reportGeneralTypeIssues]
470473
474+ if openai_message ['role' ] == 'assistant' and isinstance (
475+ contents := openai_message ['content' ], str
476+ ): # pragma: lax no cover
477+ openai_message ['content' ] = re .sub (r'<think>.*?</think>\s*' , '' , contents , flags = re .DOTALL ).strip ()
478+
471479 return openai_messages
0 commit comments