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,11 @@ field: ``{"google": {"thought_signature": "..."}}``. This MUST survive the
|
||||
parse → serialize round-trip so the model can continue reasoning.
|
||||
"""
|
||||
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from openai.types.chat import ChatCompletion, ChatCompletionChunk
|
||||
|
||||
from nanobot.providers.base import ToolCallRequest
|
||||
from nanobot.providers.openai_compat_provider import OpenAICompatProvider
|
||||
from nanobot.providers.registry import ProviderSpec
|
||||
@@ -55,26 +57,33 @@ def test_tool_call_request_omits_absent_extras() -> None:
|
||||
assert "provider_specific_fields" not in payload["function"]
|
||||
|
||||
|
||||
# ── _parse: SDK-object branch ──────────────────────────────────────────
|
||||
# ── _parse: SDK objects ────────────────────────────────────────────────
|
||||
|
||||
def _make_sdk_response_with_extra_content():
|
||||
"""Simulate a Gemini response via the OpenAI SDK (SimpleNamespace)."""
|
||||
fn = SimpleNamespace(name="get_weather", arguments='{"city":"Tokyo"}')
|
||||
tc = SimpleNamespace(
|
||||
id="call_1",
|
||||
index=0,
|
||||
type="function",
|
||||
function=fn,
|
||||
extra_content=GEMINI_EXTRA,
|
||||
)
|
||||
msg = SimpleNamespace(
|
||||
content=None,
|
||||
tool_calls=[tc],
|
||||
reasoning_content=None,
|
||||
)
|
||||
choice = SimpleNamespace(message=msg, finish_reason="tool_calls")
|
||||
usage = SimpleNamespace(prompt_tokens=10, completion_tokens=5, total_tokens=15)
|
||||
return SimpleNamespace(choices=[choice], usage=usage)
|
||||
def _make_sdk_response_with_extra_content() -> ChatCompletion:
|
||||
return ChatCompletion.model_validate({
|
||||
"id": "chatcmpl-gemini",
|
||||
"object": "chat.completion",
|
||||
"created": 0,
|
||||
"model": "gemini-3.1-pro-preview",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"finish_reason": "tool_calls",
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather", "arguments": '{"city":"Tokyo"}', "inner": "value",
|
||||
},
|
||||
"extra_content": GEMINI_EXTRA,
|
||||
"provider_tag": "opaque",
|
||||
}],
|
||||
},
|
||||
}],
|
||||
"usage": {"prompt_tokens": 10, "completion_tokens": 5, "total_tokens": 15},
|
||||
})
|
||||
|
||||
|
||||
def test_parse_sdk_object_preserves_extra_content() -> None:
|
||||
@@ -87,9 +96,13 @@ def test_parse_sdk_object_preserves_extra_content() -> None:
|
||||
tc = result.tool_calls[0]
|
||||
assert tc.name == "get_weather"
|
||||
assert tc.extra_content == GEMINI_EXTRA
|
||||
assert tc.provider_specific_fields == {"provider_tag": "opaque"}
|
||||
assert tc.function_provider_specific_fields == {"inner": "value"}
|
||||
|
||||
payload = tc.to_openai_tool_call()
|
||||
assert payload["extra_content"] == GEMINI_EXTRA
|
||||
assert payload["provider_specific_fields"] == {"provider_tag": "opaque"}
|
||||
assert payload["function"]["provider_specific_fields"] == {"inner": "value"}
|
||||
|
||||
|
||||
# ── _parse: dict/mapping branch ───────────────────────────────────────
|
||||
@@ -125,14 +138,21 @@ def test_parse_dict_preserves_extra_content() -> None:
|
||||
assert payload["extra_content"] == GEMINI_EXTRA
|
||||
|
||||
|
||||
def test_parse_dict_deduplicates_duplicate_tool_call_ids() -> None:
|
||||
@pytest.mark.parametrize("as_sdk", [False, True], ids=["dict", "sdk"])
|
||||
def test_parse_deduplicates_duplicate_tool_call_ids(as_sdk: bool) -> None:
|
||||
with patch("nanobot.providers.openai_compat_provider.AsyncOpenAI"):
|
||||
provider = OpenAICompatProvider()
|
||||
|
||||
response_dict = {
|
||||
"id": "chatcmpl-parallel",
|
||||
"object": "chat.completion",
|
||||
"created": 0,
|
||||
"model": "test-model",
|
||||
"choices": [
|
||||
{
|
||||
"index": 0,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_same",
|
||||
@@ -143,7 +163,9 @@ def test_parse_dict_deduplicates_duplicate_tool_call_ids() -> None:
|
||||
"finish_reason": "tool_calls",
|
||||
},
|
||||
{
|
||||
"index": 1,
|
||||
"message": {
|
||||
"role": "assistant",
|
||||
"content": None,
|
||||
"tool_calls": [{
|
||||
"id": "call_same",
|
||||
@@ -156,7 +178,8 @@ def test_parse_dict_deduplicates_duplicate_tool_call_ids() -> None:
|
||||
],
|
||||
}
|
||||
|
||||
result = provider._parse(response_dict)
|
||||
response = ChatCompletion.model_validate(response_dict) if as_sdk else response_dict
|
||||
result = provider._parse(response)
|
||||
|
||||
ids = [tc.id for tc in result.tool_calls]
|
||||
assert len(ids) == 2
|
||||
@@ -169,25 +192,39 @@ def test_parse_dict_deduplicates_duplicate_tool_call_ids() -> None:
|
||||
# ── _parse_chunks: streaming round-trip ───────────────────────────────
|
||||
|
||||
def test_parse_chunks_sdk_preserves_extra_content() -> None:
|
||||
fn_delta = SimpleNamespace(name="get_weather", arguments='{"city":"Tokyo"}')
|
||||
tc_delta = SimpleNamespace(
|
||||
id="call_1",
|
||||
index=0,
|
||||
function=fn_delta,
|
||||
extra_content=GEMINI_EXTRA,
|
||||
)
|
||||
delta = SimpleNamespace(content=None, tool_calls=[tc_delta])
|
||||
choice = SimpleNamespace(finish_reason="tool_calls", delta=delta)
|
||||
chunk = SimpleNamespace(choices=[choice], usage=None)
|
||||
chunk = ChatCompletionChunk.model_validate({
|
||||
"id": "chatcmpl-gemini",
|
||||
"object": "chat.completion.chunk",
|
||||
"created": 0,
|
||||
"model": "gemini-3.1-pro-preview",
|
||||
"choices": [{
|
||||
"index": 0,
|
||||
"finish_reason": "tool_calls",
|
||||
"delta": {"tool_calls": [{
|
||||
"index": 0,
|
||||
"id": "call_1",
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_weather", "arguments": '{"city":"Tokyo"}', "inner": "value",
|
||||
},
|
||||
"extra_content": GEMINI_EXTRA,
|
||||
"provider_tag": "opaque",
|
||||
}]},
|
||||
}],
|
||||
})
|
||||
|
||||
result = OpenAICompatProvider._parse_chunks([chunk])
|
||||
|
||||
assert len(result.tool_calls) == 1
|
||||
tc = result.tool_calls[0]
|
||||
assert tc.extra_content == GEMINI_EXTRA
|
||||
assert tc.provider_specific_fields == {"provider_tag": "opaque"}
|
||||
assert tc.function_provider_specific_fields == {"inner": "value"}
|
||||
|
||||
payload = tc.to_openai_tool_call()
|
||||
assert payload["extra_content"] == GEMINI_EXTRA
|
||||
assert payload["provider_specific_fields"] == {"provider_tag": "opaque"}
|
||||
assert payload["function"]["provider_specific_fields"] == {"inner": "value"}
|
||||
|
||||
|
||||
def test_parse_chunks_dict_preserves_extra_content() -> None:
|
||||
|
||||
Reference in new issue
Block a user