Files
wehub-resource-sync 97e91a83f3
Ruff / Ruff (push) Has been cancelled
Test / Core Tests (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.10) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.11) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.12) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.13) (push) Has been cancelled
Test / Offline Coverage Tests (Python 3.9) (push) Has been cancelled
Test / Full Coverage (Python 3.11) (push) Has been cancelled
Test / Core Provider Tests (OpenAI) (push) Has been cancelled
Test / Core Provider Tests (Anthropic) (push) Has been cancelled
Test / Core Provider Tests (Google) (push) Has been cancelled
Test / Core Provider Tests (Other) (push) Has been cancelled
Test / Anthropic Tests (push) Has been cancelled
Test / Gemini Tests (push) Has been cancelled
Test / Google GenAI Tests (push) Has been cancelled
Test / Vertex AI Tests (push) Has been cancelled
Test / OpenAI Tests (push) Has been cancelled
Test / Writer Tests (push) Has been cancelled
Test / Auto Client Tests (push) Has been cancelled
ty / type-check (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:36:38 +08:00

85 lines
2.4 KiB
Python

import pytest
pytest.importorskip("google.genai")
from google.genai import types
from instructor.v2.providers.gemini.utils import reask_genai_tools
def _response_with_content(content: types.Content) -> types.GenerateContentResponse:
return types.GenerateContentResponse(candidates=[types.Candidate(content=content)])
def test_reask_genai_tools_preserves_thought_signature():
function_call_part = types.Part.from_function_call(
name="test_fn", args={"value": 1}
)
function_call_part.thought_signature = b"sig"
model_content = types.Content(role="model", parts=[function_call_part])
original_kwargs = {"contents": []}
result = reask_genai_tools(
kwargs=original_kwargs,
response=_response_with_content(model_content),
exception=Exception("boom"),
)
assert original_kwargs["contents"] == []
assert result["contents"][-2] is model_content
assert result["contents"][-2].parts[0].thought_signature == b"sig"
tool_content = result["contents"][-1]
assert tool_content.role == "tool"
assert tool_content.parts[0].function_response.name == "test_fn"
def test_reask_genai_tools_finds_function_call_part_when_not_first():
function_call_part = types.Part.from_function_call(
name="test_fn", args={"value": 1}
)
model_content = types.Content(
role="model",
parts=[
types.Part.from_text(text="some preface"),
function_call_part,
],
)
result = reask_genai_tools(
kwargs={"contents": []},
response=_response_with_content(model_content),
exception=Exception("boom"),
)
assert result["contents"][-2] is model_content
assert result["contents"][-1].role == "tool"
def test_reask_genai_tools_handles_none_response():
result = reask_genai_tools(
kwargs={},
response=None,
exception=Exception("boom"),
)
assert result["contents"][-1].role == "user"
def test_reask_genai_tools_falls_back_when_no_function_call():
model_content = types.Content(
role="model",
parts=[types.Part.from_text(text="not a function call")],
)
result = reask_genai_tools(
kwargs={"contents": []},
response=_response_with_content(model_content),
exception=Exception("boom"),
)
assert result["contents"][0] is model_content
assert result["contents"][1].role == "user"