Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.2.0-alpha.43"
".": "0.2.0-alpha.44"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## 0.2.0-alpha.44 (2025-02-26)

Full Changelog: [v0.2.0-alpha.43...v0.2.0-alpha.44](https://github.com/openlayer-ai/openlayer-python/compare/v0.2.0-alpha.43...v0.2.0-alpha.44)

### Features

* feat(tracing): completes OPEN-6538 Surface root step metadata at the request level ([1bcedcf](https://github.com/openlayer-ai/openlayer-python/commit/1bcedcf57d509064f89e2a5fae3fb39f22da5920))

## 0.2.0-alpha.43 (2025-02-24)

Full Changelog: [v0.2.0-alpha.42...v0.2.0-alpha.43](https://github.com/openlayer-ai/openlayer-python/compare/v0.2.0-alpha.42...v0.2.0-alpha.43)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "openlayer"
version = "0.2.0-alpha.43"
version = "0.2.0-alpha.44"
description = "The official Python library for the openlayer API"
dynamic = ["readme"]
license = "Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion src/openlayer/_version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

__title__ = "openlayer"
__version__ = "0.2.0-alpha.43" # x-release-please-version
__version__ = "0.2.0-alpha.44" # x-release-please-version
35 changes: 28 additions & 7 deletions src/openlayer/lib/tracing/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ def add_chat_completion_step_to_trace(**kwargs) -> None:


# ----------------------------- Tracing decorator ---------------------------- #
def trace(*step_args, inference_pipeline_id: Optional[str] = None, context_kwarg: Optional[str] = None, **step_kwargs):
def trace(
*step_args,
inference_pipeline_id: Optional[str] = None,
context_kwarg: Optional[str] = None,
**step_kwargs,
):
"""Decorator to trace a function.

Examples
Expand Down Expand Up @@ -175,7 +180,9 @@ def decorator(func):
def wrapper(*func_args, **func_kwargs):
if step_kwargs.get("name") is None:
step_kwargs["name"] = func.__name__
with create_step(*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs) as step:
with create_step(
*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs
) as step:
output = exception = None
try:
output = func(*func_args, **func_kwargs)
Expand All @@ -196,7 +203,10 @@ def wrapper(*func_args, **func_kwargs):
if context_kwarg in inputs:
log_context(inputs.get(context_kwarg))
else:
logger.warning("Context kwarg `%s` not found in inputs of the current function.", context_kwarg)
logger.warning(
"Context kwarg `%s` not found in inputs of the current function.",
context_kwarg,
)

step.log(
inputs=inputs,
Expand All @@ -215,7 +225,10 @@ def wrapper(*func_args, **func_kwargs):


def trace_async(
*step_args, inference_pipeline_id: Optional[str] = None, context_kwarg: Optional[str] = None, **step_kwargs
*step_args,
inference_pipeline_id: Optional[str] = None,
context_kwarg: Optional[str] = None,
**step_kwargs,
):
"""Decorator to trace a function.

Expand Down Expand Up @@ -260,7 +273,9 @@ def decorator(func):
async def wrapper(*func_args, **func_kwargs):
if step_kwargs.get("name") is None:
step_kwargs["name"] = func.__name__
with create_step(*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs) as step:
with create_step(
*step_args, inference_pipeline_id=inference_pipeline_id, **step_kwargs
) as step:
output = exception = None
try:
output = await func(*func_args, **func_kwargs)
Expand All @@ -281,7 +296,10 @@ async def wrapper(*func_args, **func_kwargs):
if context_kwarg in inputs:
log_context(inputs.get(context_kwarg))
else:
logger.warning("Context kwarg `%s` not found in inputs of the current function.", context_kwarg)
logger.warning(
"Context kwarg `%s` not found in inputs of the current function.",
context_kwarg,
)

step.log(
inputs=inputs,
Expand All @@ -299,7 +317,9 @@ async def wrapper(*func_args, **func_kwargs):
return decorator


async def _invoke_with_context(coroutine: Awaitable[Any]) -> Tuple[contextvars.Context, Any]:
async def _invoke_with_context(
coroutine: Awaitable[Any],
) -> Tuple[contextvars.Context, Any]:
"""Runs a coroutine and preserves the context variables set within it."""
result = await coroutine
context = contextvars.copy_context()
Expand Down Expand Up @@ -356,6 +376,7 @@ def post_process_trace(
"cost": processed_steps[0].get("cost", 0),
"tokens": processed_steps[0].get("tokens", 0),
"steps": processed_steps,
**root_step.metadata,
}
if input_variables:
trace_data.update(input_variables)
Expand Down