Skip to content

vertexai|community|genai: Change the client_library_version to gapic_version in gapic client's info. #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/community/langchain_google_community/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def get_client_info(module: Optional[str] = None) -> "ClientInfo":
client_library_version, user_agent = get_user_agent(module)
return ClientInfo(
client_library_version=client_library_version,
gapic_version=client_library_version,
user_agent=user_agent,
)

Expand Down
64 changes: 64 additions & 0 deletions libs/community/tests/unit_tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import MagicMock, patch

from google.api_core.gapic_v1.client_info import ClientInfo

from langchain_google_community._utils import (
get_client_info,
get_user_agent,
)

Expand Down Expand Up @@ -29,3 +32,64 @@
client_lib_version, user_agent_str = get_user_agent(module="test-module")
assert client_lib_version == "1.2.3-test-module"
assert user_agent_str == "langchain-google-community/1.2.3-test-module"


@patch("langchain_google_community._utils.os.environ.get")
@patch("langchain_google_community._utils.metadata.version")
def test_get_client_info_with_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = True
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module+remote_reasoning_engine"

Check failure on line 46 in libs/community/tests/unit_tests/test_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/community / - / make lint #3.12

Ruff (E501)

tests/unit_tests/test_utils.py:46:89: E501 Line too long (92 > 88)
assert client_info.gapic_version == "1.2.3-test-module+remote_reasoning_engine"
assert client_info.user_agent == (
"langchain-google-community/1.2.3-test-module+remote_reasoning_engine"
)


@patch("langchain_google_community._utils.os.environ.get")
@patch("langchain_google_community._utils.metadata.version")
def test_get_client_info_without_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module"
assert client_info.gapic_version == "1.2.3-test-module"
assert client_info.user_agent == "langchain-google-community/1.2.3-test-module"


@patch("langchain_google_community._utils.os.environ.get")
@patch("langchain_google_community._utils.metadata.version")
def test_get_client_info_no_module(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info()
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3"
assert client_info.gapic_version == "1.2.3"
assert client_info.user_agent == "langchain-google-community/1.2.3"


@patch("langchain_google_community._utils.os.environ.get")
@patch("langchain_google_community._utils.metadata.PackageNotFoundError")
@patch("langchain_google_community._utils.metadata.version")
def test_get_client_info_package_not_found(
mock_version: MagicMock,
mock_package_not_found_error: MagicMock,
mock_environ_get: MagicMock,
) -> None:
mock_version.side_effect = mock_package_not_found_error
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "0.0.0-test-module"
assert client_info.gapic_version == "0.0.0-test-module"
assert client_info.user_agent == "langchain-google-community/0.0.0-test-module"
1 change: 1 addition & 0 deletions libs/genai/langchain_google_genai/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def get_client_info(module: Optional[str] = None) -> "ClientInfo":
client_library_version, user_agent = get_user_agent(module)
return ClientInfo(
client_library_version=client_library_version,
gapic_version=client_library_version,
user_agent=user_agent,
)

Expand Down
64 changes: 64 additions & 0 deletions libs/genai/tests/unit_tests/test_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from unittest.mock import MagicMock, patch

from google.api_core.gapic_v1.client_info import ClientInfo

from langchain_google_genai._common import (
get_client_info,
get_user_agent,
)

Expand Down Expand Up @@ -29,3 +32,64 @@
client_lib_version, user_agent_str = get_user_agent(module="test-module")
assert client_lib_version == "1.2.3-test-module"
assert user_agent_str == "langchain-google-genai/1.2.3-test-module"


@patch("langchain_google_genai._common.os.environ.get")
@patch("langchain_google_genai._common.metadata.version")
def test_get_client_info_with_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = True
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module+remote_reasoning_engine"

Check failure on line 46 in libs/genai/tests/unit_tests/test_common.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.9

Ruff (E501)

tests/unit_tests/test_common.py:46:89: E501 Line too long (92 > 88)

Check failure on line 46 in libs/genai/tests/unit_tests/test_common.py

View workflow job for this annotation

GitHub Actions / cd libs/genai / - / make lint #3.12

Ruff (E501)

tests/unit_tests/test_common.py:46:89: E501 Line too long (92 > 88)
assert client_info.gapic_version == "1.2.3-test-module+remote_reasoning_engine"
assert client_info.user_agent == (
"langchain-google-genai/1.2.3-test-module+remote_reasoning_engine"
)


@patch("langchain_google_genai._common.os.environ.get")
@patch("langchain_google_genai._common.metadata.version")
def test_get_client_info_without_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module"
assert client_info.gapic_version == "1.2.3-test-module"
assert client_info.user_agent == "langchain-google-genai/1.2.3-test-module"


@patch("langchain_google_genai._common.os.environ.get")
@patch("langchain_google_genai._common.metadata.version")
def test_get_client_info_no_module(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info()
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3"
assert client_info.gapic_version == "1.2.3"
assert client_info.user_agent == "langchain-google-genai/1.2.3"


@patch("langchain_google_genai._common.os.environ.get")
@patch("langchain_google_genai._common.metadata.PackageNotFoundError")
@patch("langchain_google_genai._common.metadata.version")
def test_get_client_info_package_not_found(
mock_version: MagicMock,
mock_package_not_found_error: MagicMock,
mock_environ_get: MagicMock,
) -> None:
mock_version.side_effect = mock_package_not_found_error
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "0.0.0-test-module"
assert client_info.gapic_version == "0.0.0-test-module"
assert client_info.user_agent == "langchain-google-genai/0.0.0-test-module"
1 change: 1 addition & 0 deletions libs/vertexai/langchain_google_vertexai/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def get_client_info(module: Optional[str] = None) -> "ClientInfo":
client_library_version, user_agent = get_user_agent(module)
return ClientInfo(
client_library_version=client_library_version,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's keep both, please

we might rely on client_library_version in some other place

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just send both client_library_version and additionally gapic_version= client_library_version

gapic_version=client_library_version,
user_agent=user_agent,
)

Expand Down
63 changes: 63 additions & 0 deletions libs/vertexai/tests/unit_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

import pytest
from google.api_core.exceptions import ClientError, GoogleAPICallError, InvalidArgument
from google.api_core.gapic_v1.client_info import ClientInfo

from langchain_google_vertexai._retry import create_base_retry_decorator
from langchain_google_vertexai._utils import (
GoogleModelFamily,
_get_def_key_from_schema_path,
get_client_info,
get_user_agent,
replace_defs_in_schema,
)
Expand Down Expand Up @@ -210,3 +212,64 @@
client_lib_version, user_agent_str = get_user_agent(module="test-module")
assert client_lib_version == "1.2.3-test-module"
assert user_agent_str == "langchain-google-vertexai/1.2.3-test-module"


@patch("langchain_google_vertexai._utils.os.environ.get")
@patch("langchain_google_vertexai._utils.metadata.version")
def test_get_client_info_with_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = True
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module+remote_reasoning_engine"

Check failure on line 226 in libs/vertexai/tests/unit_tests/test_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/vertexai / - / make lint #3.9

Ruff (E501)

tests/unit_tests/test_utils.py:226:89: E501 Line too long (92 > 88)

Check failure on line 226 in libs/vertexai/tests/unit_tests/test_utils.py

View workflow job for this annotation

GitHub Actions / cd libs/vertexai / - / make lint #3.12

Ruff (E501)

tests/unit_tests/test_utils.py:226:89: E501 Line too long (92 > 88)
assert client_info.gapic_version == "1.2.3-test-module+remote_reasoning_engine"
assert client_info.user_agent == (
"langchain-google-vertexai/1.2.3-test-module+remote_reasoning_engine"
)


@patch("langchain_google_vertexai._utils.os.environ.get")
@patch("langchain_google_vertexai._utils.metadata.version")
def test_get_client_info_without_telemetry_env_variable(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3-test-module"
assert client_info.gapic_version == "1.2.3-test-module"
assert client_info.user_agent == "langchain-google-vertexai/1.2.3-test-module"


@patch("langchain_google_vertexai._utils.os.environ.get")
@patch("langchain_google_vertexai._utils.metadata.version")
def test_get_client_info_no_module(
mock_version: MagicMock, mock_environ_get: MagicMock
) -> None:
mock_version.return_value = "1.2.3"
mock_environ_get.return_value = False
client_info = get_client_info()
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "1.2.3"
assert client_info.gapic_version == "1.2.3"
assert client_info.user_agent == "langchain-google-vertexai/1.2.3"


@patch("langchain_google_vertexai._utils.os.environ.get")
@patch("langchain_google_vertexai._utils.metadata.PackageNotFoundError")
@patch("langchain_google_vertexai._utils.metadata.version")
def test_get_client_info_package_not_found(
mock_version: MagicMock,
mock_package_not_found_error: MagicMock,
mock_environ_get: MagicMock,
) -> None:
mock_version.side_effect = mock_package_not_found_error
mock_environ_get.return_value = False
client_info = get_client_info(module="test-module")
assert isinstance(client_info, ClientInfo)
assert client_info.client_library_version == "0.0.0-test-module"
assert client_info.gapic_version == "0.0.0-test-module"
assert client_info.user_agent == "langchain-google-vertexai/0.0.0-test-module"
Loading