Skip to content

Commit

Permalink
genai: update standard tests library (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
efriis authored Feb 20, 2025
1 parent 80257cd commit 008b9e9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 42 deletions.
25 changes: 17 additions & 8 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,12 @@ def _convert_to_parts(
return parts


def _convert_tool_message_to_part(message: ToolMessage | FunctionMessage) -> Part:
def _convert_tool_message_to_part(
message: ToolMessage | FunctionMessage, name: Optional[str] = None
) -> Part:
"""Converts a tool or function message to a google part."""
# Legacy agent stores tool name in message.additional_kwargs instead of message.name
name = message.name or message.additional_kwargs.get("name")
name = message.name or name or message.additional_kwargs.get("name")
response: Any
if not isinstance(message.content, str):
response = message.content
Expand Down Expand Up @@ -332,16 +334,17 @@ def _get_ai_message_tool_messages_parts(
list of Parts.
"""
# We are interested only in the tool messages that are part of the AI message
tool_calls_ids = [tool_call["id"] for tool_call in ai_message.tool_calls]
tool_calls_ids = {tool_call["id"]: tool_call for tool_call in ai_message.tool_calls}
parts = []
for i, message in enumerate(tool_messages):
if not tool_calls_ids:
break
if message.tool_call_id in tool_calls_ids:
# remove the id from the list, so that we do not iterate over it again
tool_calls_ids.remove(message.tool_call_id)
part = _convert_tool_message_to_part(message)
tool_call = tool_calls_ids[message.tool_call_id]
part = _convert_tool_message_to_part(message, name=tool_call.get("name"))
parts.append(part)
# remove the id from the dict, so that we do not iterate over it again
tool_calls_ids.pop(message.tool_call_id)
return parts


Expand All @@ -361,8 +364,14 @@ def _parse_chat_history(
message for message in input_messages if isinstance(message, ToolMessage)
]
for i, message in enumerate(messages_without_tool_messages):
if i == 0 and isinstance(message, SystemMessage):
system_instruction = Content(parts=_convert_to_parts(message.content))
if isinstance(message, SystemMessage):
system_parts = _convert_to_parts(message.content)
if i == 0:
system_instruction = Content(parts=system_parts)
elif system_instruction is not None:
system_instruction.parts.extend(system_parts)
else:
pass
continue
elif isinstance(message, AIMessage):
role = "model"
Expand Down
40 changes: 30 additions & 10 deletions libs/genai/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 2 additions & 12 deletions libs/genai/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ license = "MIT"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
langchain-core = "^0.3.27"
langchain-core = "^0.3.35"
google-generativeai = "^0.8.0"
pydantic = ">=2,<3"
filetype = "^1.2.0"
Expand All @@ -28,41 +28,33 @@ syrupy = "^4.0.2"
pytest-watcher = "^0.3.4"
pytest-asyncio = "^0.21.1"
numpy = "^1.26.2"
langchain-tests = "0.3.1"
langchain-tests = "0.3.12"

[tool.codespell]
ignore-words-list = "rouge"




[tool.poetry.group.codespell]
optional = true

[tool.poetry.group.codespell.dependencies]
codespell = "^2.2.0"




[tool.poetry.group.test_integration]
optional = true

[tool.poetry.group.test_integration.dependencies]
pytest = "^7.3.0"




[tool.poetry.group.lint]
optional = true

[tool.poetry.group.lint.dependencies]
ruff = "^0.1.5"




[tool.poetry.group.typing.dependencies]
mypy = "^1.10"
types-requests = "^2.28.11.5"
Expand All @@ -71,8 +63,6 @@ types-protobuf = "^4.24.0.20240302"
numpy = "^1.26.2"




[tool.poetry.group.dev]
optional = true

Expand Down
43 changes: 32 additions & 11 deletions libs/genai/tests/integration_tests/test_standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from langchain_core.language_models import BaseChatModel
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.rate_limiters import InMemoryRateLimiter
from langchain_standard_tests.integration_tests import ChatModelIntegrationTests
from langchain_core.tools import BaseTool
from langchain_tests.integration_tests import ChatModelIntegrationTests

from langchain_google_genai import ChatGoogleGenerativeAI

Expand All @@ -26,18 +27,30 @@ def chat_model_params(self) -> dict:
"rate_limiter": rate_limiter_2_0,
}

@pytest.mark.xfail(
reason="Likely a bug in genai: prompt_token_count inconsistent in final chunk."
)
def test_usage_metadata_streaming(self, model: BaseChatModel) -> None:
super().test_usage_metadata_streaming(model)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
async def test_structured_output_async(self, model: BaseChatModel) -> None:
await super().test_structured_output_async(model)
async def test_structured_output_async(
self, model: BaseChatModel, schema_type: str
) -> None:
await super().test_structured_output_async(model, schema_type)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output(self, model: BaseChatModel) -> None:
super().test_structured_output(model)
def test_structured_output(self, model: BaseChatModel, schema_type: str) -> None:
super().test_structured_output(model, schema_type)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output_pydantic_2_v1(self, model: BaseChatModel) -> None:
super().test_structured_output_pydantic_2_v1(model)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output_optional_param(self, model: BaseChatModel) -> None:
super().test_structured_output_optional_param(model)

@pytest.mark.xfail(reason="investigate")
def test_bind_runnables_as_tools(self, model: BaseChatModel) -> None:
super().test_bind_runnables_as_tools(model)
Expand All @@ -56,20 +69,28 @@ def chat_model_params(self) -> dict:
}

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
async def test_structured_output_async(self, model: BaseChatModel) -> None:
await super().test_structured_output_async(model)
async def test_structured_output_async(
self, model: BaseChatModel, schema_type: str
) -> None:
await super().test_structured_output_async(model, schema_type)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output(self, model: BaseChatModel) -> None:
super().test_structured_output(model)
def test_structured_output(self, model: BaseChatModel, schema_type: str) -> None:
super().test_structured_output(model, schema_type)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output_pydantic_2_v1(self, model: BaseChatModel) -> None:
super().test_structured_output_pydantic_2_v1(model)

@pytest.mark.xfail(reason="with_structured_output with JSON schema not supported.")
def test_structured_output_optional_param(self, model: BaseChatModel) -> None:
super().test_structured_output_optional_param(model)

@pytest.mark.xfail(reason="Not yet supported")
def test_tool_message_histories_list_content(self, model: BaseChatModel) -> None:
super().test_tool_message_histories_list_content(model)
def test_tool_message_histories_list_content(
self, model: BaseChatModel, my_adder_tool: BaseTool
) -> None:
super().test_tool_message_histories_list_content(model, my_adder_tool)

@property
def supported_usage_metadata_details(
Expand Down
2 changes: 1 addition & 1 deletion libs/genai/tests/unit_tests/test_standard.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Tuple, Type

from langchain_core.language_models import BaseChatModel
from langchain_standard_tests.unit_tests import ChatModelUnitTests
from langchain_tests.unit_tests import ChatModelUnitTests

from langchain_google_genai import ChatGoogleGenerativeAI

Expand Down

0 comments on commit 008b9e9

Please sign in to comment.