Skip to content
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,4 @@ Users can be now overwrite the input and ouput attributes of spans created by in

- Added utility to set input and output data for any active span in a trace

[0.1.86]: https://github.com/KeyValueSoftwareSystems/netra-sdk-py/tree/main
[0.1.89]: https://github.com/KeyValueSoftwareSystems/netra-sdk-py/tree/main
77 changes: 77 additions & 0 deletions netra/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,20 @@ def shutdown(cls) -> None:
except Exception:
pass

# Close simulation HTTP client
if hasattr(cls, "simulation") and cls.simulation is not None:
try:
cls.simulation.close()
except Exception:
pass

# Close evaluation HTTP client
if hasattr(cls, "evaluation") and cls.evaluation is not None:
try:
cls.evaluation.close()
except Exception:
pass

@classmethod
def get_meter(cls, name: str = "netra", version: Optional[str] = None) -> otel_metrics.Meter:
"""
Expand Down Expand Up @@ -409,6 +423,47 @@ def set_custom_event(cls, event_name: str, attributes: Any) -> None:
else:
logger.warning("Both event_name and attributes must be provided for custom events.")

@classmethod
def record_exception(
cls,
exception: BaseException,
attributes: Optional[Dict[str, Any]] = None,
) -> None:
"""Record a caught exception on the currently active span.

Use this inside ``except`` blocks to attach exception details to the
current span when the exception is being handled and will not propagate
to the SDK's automatic capture logic.

The method adds a standard OpenTelemetry exception event (with type,
message, and stacktrace), sets the span status to ERROR, and records
the ``netra.error_message`` attribute for consistency with the rest of
the SDK.

Args:
exception: The exception instance to record.
attributes: Optional extra attributes to attach to the exception
event.

Example::

@workflow
def process_order(order_id: str) -> str:
try:
result = call_payment_api(order_id)
except PaymentError as exc:
Netra.record_exception(exc)
return "fallback_result"
return result
"""
if not isinstance(exception, BaseException):
logger.error(
"record_exception: exception must be a BaseException instance, got %s",
type(exception),
)
return
SessionManager.record_exception(exception, attributes=attributes)

@classmethod
def add_conversation(cls, conversation_type: ConversationType, role: str, content: Any) -> None:
"""
Expand Down Expand Up @@ -461,6 +516,28 @@ def set_root_output(cls, value: Any) -> None:
"""
SessionManager.set_root_output(value)

@classmethod
def set_root_output_stream(cls, value: Any) -> Any:
"""
Wrap a stream so the accumulated output is set on the root span when iteration ends.

The returned object is a transparent proxy — iterate over it instead of the original::

stream = Netra.set_root_output_stream(stream)
for chunk in stream:
...

Supports both sync and async iterables. Returns *value* unchanged if no active trace
context exists or if *value* is not iterable.

Args:
value: The stream to wrap (Netra-instrumented or any generic iterable).

Returns:
A wrapped stream proxy, or *value* unchanged if wrapping is not possible.
"""
return SessionManager.set_root_output_stream(value)

@classmethod
def start_span(
cls,
Expand Down
4 changes: 4 additions & 0 deletions netra/evaluation/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from netra.evaluation.api import Evaluation
from netra.evaluation.evaluator import BaseEvaluator
from netra.evaluation.models import (
Dataset,
DatasetItem,
EvaluatorConfig,
EvaluatorContext,
EvaluatorOutput,
LocalDataset,
ScoreType,
TurnType,
)

__all__ = [
"Evaluation",
"Dataset",
"TurnType",
"DatasetItem",
"BaseEvaluator",
"EvaluatorContext",
Expand Down
Loading