refactor(providers): remove duplicate chat response parsers
This commit is contained in:
1 parent
0d09b3dc09
commit
e4fc8788c4
9 files changed
+482
-572
No files matched your search
@@ -5,9 +5,10 @@ the model is in thinking mode and 'content' is empty. This test module
|
||||
verifies the fallback logic for all code paths.
|
||||
"""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from openai.types.chat import ChatCompletion, ChatCompletionChunk
|
||||
|
||||
from nanobot.providers.openai_compat_provider import OpenAICompatProvider
|
||||
from nanobot.providers.registry import ProviderSpec
|
||||
|
||||
@@ -71,17 +72,26 @@ def test_parse_dict_stepfun_reasoning_priority() -> None:
|
||||
assert result.reasoning_content == "formal reasoning content"
|
||||
|
||||
|
||||
# ── _parse: SDK object branch ───────────────────────────────────────────────
|
||||
# ── _parse: SDK objects ────────────────────────────────────────────────────
|
||||
|
||||
|
||||
def _make_sdk_message(content, reasoning=None, reasoning_content=None):
|
||||
"""Create a mock SDK message object."""
|
||||
msg = SimpleNamespace(content=content, tool_calls=None)
|
||||
if reasoning is not None:
|
||||
msg.reasoning = reasoning
|
||||
if reasoning_content is not None:
|
||||
msg.reasoning_content = reasoning_content
|
||||
return msg
|
||||
def _make_sdk_response(content, reasoning=None, reasoning_content=None) -> ChatCompletion:
|
||||
return ChatCompletion.model_validate({
|
||||
"id": "chatcmpl-stepfun",
|
||||
"object": "chat.completion",
|
||||
"created": 0,
|
||||
"model": "step-3.5-flash",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"finish_reason": "stop",
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": content,
|
||||
"reasoning": reasoning,
|
||||
"reasoning_content": reasoning_content,
|
||||
},
|
||||
}],
|
||||
})
|
||||
|
||||
|
||||
def test_parse_sdk_stepfun_reasoning_fallback() -> None:
|
||||
@@ -89,9 +99,7 @@ def test_parse_sdk_stepfun_reasoning_fallback() -> None:
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = OpenAICompatProvider(spec=_STEPFUN_SPEC)
|
||||
|
||||
msg = _make_sdk_message(content=None, reasoning="After analysis: result is 4.")
|
||||
choice = SimpleNamespace(finish_reason="stop", message=msg)
|
||||
response = SimpleNamespace(choices=[choice], usage=None)
|
||||
response = _make_sdk_response(content=None, reasoning="After analysis: result is 4.")
|
||||
|
||||
result = provider._parse(response)
|
||||
|
||||
@@ -104,13 +112,11 @@ def test_parse_sdk_stepfun_reasoning_priority() -> None:
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = OpenAICompatProvider(spec=_STEPFUN_SPEC)
|
||||
|
||||
msg = _make_sdk_message(
|
||||
response = _make_sdk_response(
|
||||
content=None,
|
||||
reasoning="thinking process",
|
||||
reasoning_content="formal reasoning"
|
||||
reasoning_content="formal reasoning",
|
||||
)
|
||||
choice = SimpleNamespace(finish_reason="stop", message=msg)
|
||||
response = SimpleNamespace(choices=[choice], usage=None)
|
||||
|
||||
result = provider._parse(response)
|
||||
|
||||
@@ -218,19 +224,27 @@ def test_parse_chunks_dict_reasoning_precedence() -> None:
|
||||
assert result.reasoning_content == "formal: "
|
||||
|
||||
|
||||
# ── _parse_chunks: streaming SDK-object branch ─────────────────────────────
|
||||
# ── _parse_chunks: streaming SDK objects ───────────────────────────────────
|
||||
|
||||
|
||||
def _make_sdk_chunk(reasoning_content=None, reasoning=None, content=None, finish=None):
|
||||
"""Create a mock SDK chunk object."""
|
||||
delta = SimpleNamespace(
|
||||
content=content,
|
||||
reasoning_content=reasoning_content,
|
||||
reasoning=reasoning,
|
||||
tool_calls=None,
|
||||
)
|
||||
choice = SimpleNamespace(finish_reason=finish, delta=delta)
|
||||
return SimpleNamespace(choices=[choice], usage=None)
|
||||
def _make_sdk_chunk(
|
||||
reasoning_content=None, reasoning=None, content=None, finish=None,
|
||||
) -> ChatCompletionChunk:
|
||||
return ChatCompletionChunk.model_validate({
|
||||
"id": "chatcmpl-stepfun",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": 0,
|
||||
"model": "step-3.5-flash",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"finish_reason": finish,
|
||||
"delta": {
|
||||
"content": content,
|
||||
"reasoning_content": reasoning_content,
|
||||
"reasoning": reasoning,
|
||||
},
|
||||
}],
|
||||
})
|
||||
|
||||
|
||||
def test_parse_chunks_sdk_stepfun_reasoning_fallback() -> None:
|
||||
@@ -289,9 +303,7 @@ def test_parse_sdk_non_stepfun_no_reasoning_as_content() -> None:
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = OpenAICompatProvider()
|
||||
|
||||
msg = _make_sdk_message(content=None, reasoning="internal monologue")
|
||||
choice = SimpleNamespace(finish_reason="stop", message=msg)
|
||||
response = SimpleNamespace(choices=[choice], usage=None)
|
||||
response = _make_sdk_response(content=None, reasoning="internal monologue")
|
||||
|
||||
result = provider._parse(response)
|
||||
|
||||
|
||||
Reference in new issue
Block a user