Skip to content

Commit

Permalink
include cleanlab-codex version in headers for codex backend requests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelsey-wong committed Mar 3, 2025
1 parent f67bde8 commit d63d789
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/cleanlab_codex/utils/analytics.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
from __future__ import annotations

from cleanlab_codex.__about__ import __version__ as package_version


class AnalyticsMetadata:
def __init__(self, *, integration_type: str, source: str = "codex-python-sdk"):
def __init__(
self, *, integration_type: str, package_version: str = package_version, source: str = "cleanlab-codex-python"
):
self._integration_type = integration_type
self._package_version = package_version
self._source = source

def to_headers(self) -> dict[str, str]:
return {
"X-Integration-Type": self._integration_type,
"X-Client-Library-Version": self._package_version,
"X-Source": self._source,
}
4 changes: 3 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from codex.types.projects.access_key_retrieve_project_id_response import AccessKeyRetrieveProjectIDResponse
from codex.types.projects.entry import Entry as SDKEntry

from cleanlab_codex.__about__ import __version__ as package_version
from cleanlab_codex.project import MissingProjectError, Project
from cleanlab_codex.types.entry import EntryCreate

Expand Down Expand Up @@ -137,7 +138,8 @@ def test_query_read_only(mock_client_from_access_key: MagicMock) -> None:
res = project.query("What is the capital of France?", read_only=True)
expected_headers = {
"X-Integration-Type": "backup",
"X-Source": "codex-python-sdk",
"X-Source": "cleanlab-codex-python",
"X-Client-Library-Version": package_version,
}
mock_client_from_access_key.projects.entries.query.assert_called_once_with(
FAKE_PROJECT_ID, question="What is the capital of France?", extra_headers=expected_headers
Expand Down
22 changes: 17 additions & 5 deletions tests/utils/test_analytics.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
from __future__ import annotations

import pytest

from cleanlab_codex.__about__ import __version__ as package_version
from cleanlab_codex.utils.analytics import AnalyticsMetadata


def test_analytics_metadata_to_headers_uses_default_source() -> None:
def test_analytics_metadata_to_headers_uses_defaults() -> None:
metadata = AnalyticsMetadata(integration_type="backup")
assert metadata.to_headers() == {"X-Integration-Type": "backup", "X-Source": "codex-python-sdk"}

assert metadata.to_headers() == {
"X-Integration-Type": "backup",
"X-Source": "cleanlab-codex-python",
"X-Client-Library-Version": package_version,
}


def test_analytics_metadata_to_headers_uses_integration_type() -> None:
metadata = AnalyticsMetadata(integration_type="tool-call", source="test")
assert metadata.to_headers() == {"X-Integration-Type": "tool-call", "X-Source": "test"}
def test_analytics_metadata_to_headers_uses_custom_values() -> None:
metadata = AnalyticsMetadata(integration_type="tool-call", source="test", package_version="2.0.0")
assert metadata.to_headers() == {
"X-Integration-Type": "tool-call",
"X-Source": "test",
"X-Client-Library-Version": "2.0.0",
}


def test_analytics_metadata_requires_integration_type() -> None:
Expand Down

0 comments on commit d63d789

Please sign in to comment.