-
Notifications
You must be signed in to change notification settings - Fork 16
feat(integrations): add TypeSafe instrumentation #777
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
py/src/braintrust/integrations/auto_test_scripts/test_auto_typesafe.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| """Test auto_instrument for TypeSafe.""" | ||
|
|
||
| import os | ||
|
|
||
| from braintrust.auto import auto_instrument | ||
| from braintrust.integrations.test_utils import autoinstrument_test_context | ||
| from typesafe_sdk import Noul, TypeSafeClient | ||
|
|
||
|
|
||
| results = auto_instrument() | ||
| assert results.get("typesafe") is True | ||
| assert auto_instrument().get("typesafe") is True | ||
|
|
||
| with autoinstrument_test_context("test_auto_typesafe", integration="typesafe") as memory_logger: | ||
| with TypeSafeClient(api_key=os.environ["TYPESAFE_API_KEY"]) as client: | ||
| response = client.system_one( | ||
| state="The package arrived intact and on time.", | ||
| questions={"positive": Noul(instructions="Is this feedback positive?")}, | ||
| ) | ||
| assert 0 <= response.nouls["positive"].noul <= 1 | ||
|
|
||
| spans = memory_logger.pop() | ||
| assert len(spans) == 1 | ||
| span = spans[0] | ||
| assert span["metadata"]["provider"] == "typesafe" | ||
| assert span["metadata"]["model"].startswith("jev-") | ||
| assert span["input"]["questions"][0]["id"] == "positive" | ||
| assert span["output"]["answers"][0]["id"] == "positive" | ||
|
|
||
| print("SUCCESS") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| """Braintrust integration for the TypeSafe Python SDK.""" | ||
|
|
||
| from typing import Any | ||
|
|
||
| from .integration import TypeSafeIntegration | ||
| from .patchers import AsyncSystemOnePatcher, SystemOnePatcher | ||
|
|
||
|
|
||
| def setup_typesafe() -> bool: | ||
| """Instrument installed TypeSafe sync and async clients.""" | ||
| return TypeSafeIntegration.setup() | ||
|
|
||
|
|
||
| def wrap_typesafe(client: Any) -> Any: | ||
| """Instrument a TypeSafe sync or async client in place.""" | ||
| from typesafe_sdk import AsyncTypeSafeClient | ||
|
|
||
| patcher = AsyncSystemOnePatcher if isinstance(client, AsyncTypeSafeClient) else SystemOnePatcher | ||
| return patcher.wrap_target(client) | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "TypeSafeIntegration", | ||
| "setup_typesafe", | ||
| "wrap_typesafe", | ||
| ] | ||
45 changes: 45 additions & 0 deletions
45
py/src/braintrust/integrations/typesafe/cassettes/latest/test_auto_typesafe.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
...aintrust/integrations/typesafe/cassettes/latest/test_setup_typesafe_system_one_async.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
45 changes: 45 additions & 0 deletions
45
...braintrust/integrations/typesafe/cassettes/latest/test_wrap_typesafe_system_one_sync.yaml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """TypeSafe integration orchestration.""" | ||
|
|
||
| from braintrust.integrations.base import BaseIntegration | ||
|
|
||
| from .patchers import TypeSafePatcher | ||
|
|
||
|
|
||
| class TypeSafeIntegration(BaseIntegration): | ||
| """Braintrust instrumentation for the TypeSafe Python SDK.""" | ||
|
|
||
| name = "typesafe" | ||
| import_names = ("typesafe_sdk",) | ||
| distribution_names = ("typesafe-sdk",) | ||
| min_version = "0.6.0" | ||
| patchers = (TypeSafePatcher,) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| """Patchers for TypeSafe sync and async System One calls.""" | ||
|
|
||
| from braintrust.integrations.base import CompositeFunctionWrapperPatcher, FunctionWrapperPatcher | ||
|
|
||
| from .tracing import _async_system_one_wrapper, _system_one_wrapper | ||
|
|
||
|
|
||
| class SystemOnePatcher(FunctionWrapperPatcher): | ||
| name = "typesafe.system_one" | ||
| target_module = "typesafe_sdk._core.client.sync.client" | ||
| target_path = "TypeSafeClient.system_one" | ||
| wrapper = _system_one_wrapper | ||
|
|
||
|
|
||
| class AsyncSystemOnePatcher(FunctionWrapperPatcher): | ||
| name = "typesafe.async.system_one" | ||
| target_module = "typesafe_sdk._core.client.aio.client" | ||
| target_path = "AsyncTypeSafeClient.system_one" | ||
| wrapper = _async_system_one_wrapper | ||
|
|
||
|
|
||
| class TypeSafePatcher(CompositeFunctionWrapperPatcher): | ||
| name = "typesafe.system_one.all" | ||
| sub_patchers = (SystemOnePatcher, AsyncSystemOnePatcher) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
setup_typesafe()orauto_instrument()has already patchedTypeSafeClient.system_one, this new instance-scoped path callswrap_target()on a client with no instance marker and wraps the already-wrapped inherited class method again. Applications that combine global setup withwrap_typesafe()therefore emit two nested spans for every request. Check the patch marker on the class method before applying the instance wrapper, as the OpenAI integration's manual wrapping path does.Useful? React with 👍 / 👎.