API Reference
This section provides detailed technical documentation for the Trustwise SDK. For conceptual information and best practices, see the individual metric sections.
SDK
- class trustwise.sdk.TrustwiseSDK(config)[source]
Bases:
objectMain SDK class for Trustwise API access.
Provides access to all metrics through version-specific paths.
- Parameters:
config (TrustwiseConfig)
- __init__(config)[source]
Initialize the Trustwise SDK with path-based versioning support.
- Parameters:
config (
TrustwiseConfig) – Trustwise configuration instance.- Return type:
None
- get_beta_features()[source]
Get the set of features currently in beta.
- Returns:
Set of feature names that are in beta
- Return type:
Set[str]
- guardrails(thresholds, *, block_on_failure=False, callbacks=None)[source]
Create a guardrail system for response evaluation.
- Parameters:
- Return type:
- Returns:
A configured Guardrail instance.
- Raises:
ValueError – If any metric in thresholds is not supported by the current API version.
- class trustwise.sdk.TrustwiseSDKAsync(config)[source]
Bases:
objectAsync SDK entrypoint for Trustwise. Use this class to access async metrics.
- Parameters:
config (TrustwiseConfig)
- __init__(config)[source]
Initialize the Trustwise SDK with path-based versioning support.
- Parameters:
config (
TrustwiseConfig) – Trustwise configuration instance.- Return type:
None
Configuration
Simple configuration for Trustwise API with environment variable support.
- class trustwise.sdk.config.TrustwiseConfig(**data)[source]
Bases:
BaseModelConfiguration for Trustwise API.
This class handles configuration for the Trustwise API, including authentication and endpoint URLs. It supports both direct initialization and environment variables.
- Parameters:
api_key (str, Optional) – API key for authentication. If not provided, will be read from TW_API_KEY environment variable. Required for API access.
base_url (str, Optional) – Base URL for API endpoints. If not provided, will be read from TW_BASE_URL environment variable or default to https://api.trustwise.ai.
- Environment Variables:
TW_API_KEY: API key for authentication
TW_BASE_URL: Base URL for API endpoints (defaults to https://api.trustwise.ai)
Example
>>> from trustwise.sdk.config import TrustwiseConfig >>> # Using environment variables >>> config = TrustwiseConfig() >>> # Using direct initialization >>> config = TrustwiseConfig(api_key="your-api-key", base_url="https://api.trustwise.ai")
- Raises:
ValueError – If API key is missing or invalid, or if base URL is invalid
- Parameters:
- __init__(**data)[source]
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- Return type:
None
Types
Note
V4 types are now the default for all metrics. These provide enhanced type safety and improved response structures.
Types for v4 metrics.
- class trustwise.sdk.metrics.v4.types.Score(**data)[source]
Bases:
SDKBaseModelA score value between 0 and 100.
- Parameters:
value (float) – Score value between 0 and 100.
- class trustwise.sdk.metrics.v4.types.ObjectStyleScore(**data)[source]
Bases:
SDKBaseModelObject style score with label.
- class trustwise.sdk.metrics.v4.types.ContextChunk(**data)[source]
Bases:
SDKBaseModelA single context chunk for v4 context relevancy evaluation.
- Parameters:
- class trustwise.sdk.metrics.v4.types.ContextRelevancyRequest(**data)[source]
Bases:
SDKRequestModelRequest type for v4 context relevancy evaluation.
- Parameters:
metadata (dict) – Optional metadata to be returned in the response.
query (str) – The input query string.
context (list) – A non-empty list of context chunks.
severity (float | None) – (Optional) severity level (0-1).
include_chunk_scores (bool | None) – (Optional) Whether to include individual chunk scores in the response.
-
context:
list[ContextChunk]
- class trustwise.sdk.metrics.v4.types.PromptManipulationRequest(**data)[source]
Bases:
SDKRequestModelRequest type for v4 prompt manipulation evaluation.
- Parameters:
- class trustwise.sdk.metrics.v4.types.PromptManipulationResponse(**data)[source]
Bases:
SDKResponseModelResponse type for prompt manipulation detection.
Higher scores indicate a greater likelihood that the text contains prompt manipulation attempts that could manipulate the AI system.
- Parameters:
-
scores:
list[ObjectStyleScore]
- class trustwise.sdk.metrics.v4.types.AnswerRelevancyRequest(**data)[source]
Bases:
SDKRequestModelRequest type for answer relevancy evaluation.
- Parameters:
- class trustwise.sdk.metrics.v4.types.AnswerRelevancyResponse(**data)[source]
Bases:
SDKResponseModelResponse type for answer relevancy evaluation.
Answer relevancy measures how well the response addresses the specific query. A higher score indicates the response is more directly relevant to the question asked.
- Parameters:
- class trustwise.sdk.metrics.v4.types.FaithfulnessRequest(**data)[source]
Bases:
SDKRequestModelRequest type for faithfulness evaluation.
- Parameters:
metadata (dict[str, object] | None) – Optional metadata for the request that will be included in the response.
query (str) – The input query string.
response (str) – The response to evaluate.
context (list) – A non-empty list of context chunks.
severity (float | None) – (Optional) severity level (0-1).
include_citations (bool | None) – Whether to include citations in the response. If true, a unique chunk_id must additionally be provided for each context chunk.
-
context:
list[ContextChunk]
- class trustwise.sdk.metrics.v4.types.Statement(**data)[source]
Bases:
SDKBaseModelA verified fact extracted from a response.
- Parameters:
statement (str) – The extracted statement from the response text that represents a ‘atomically’ fact
label (str) – The label indicating the fact’s hallucination status. One of (‘Safe’, ‘Unsafe’, ‘Intrinsic Hallucination’, ‘Extrinsic Hallucination’)
probability (float) – The associated probability of the label (0-1)
sentence_span (list) – A list of two integers [start, end] indicating the character positions of this statement in the response text
- class trustwise.sdk.metrics.v4.types.FaithfulnessResponse(**data)[source]
Bases:
SDKBaseModelResponse type for faithfulness evaluation.
Faithfulness measures how well the response adheres to the provided context. A higher score indicates better alignment with the source material.
- Parameters:
score (float) – Faithfulness score (0-100). A higher score, indicating a better response, is correlated with a higher confidence in each statement being true, and also a higher proportion of statements being true with respect to the context.
statements (list) – List of extracted ‘atomic’ statements from the response, each containing the statement, label, associated probability, and location in the text.
- class trustwise.sdk.metrics.v4.types.ContextRelevancyResponse(**data)[source]
Bases:
SDKResponseModelResponse type for context relevancy evaluation.
Context relevancy measures how well the provided context is relevant to the query. A higher score indicates the context is more directly relevant to the question asked.
- Parameters:
-
scores:
list[ObjectStyleScore]
- class trustwise.sdk.metrics.v4.types.ClarityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for clarity evaluation.
Clarity measures how easy text is to read. It gives higher scores to writing that contains words which are easier to read, and uses concise, self-contained sentences. It does not measure how well you understand the ideas in the text.
- Parameters:
- class trustwise.sdk.metrics.v4.types.ClarityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for clarity evaluation.
Clarity scores indicate how clear and understandable the response is. Higher scores suggest better readability and comprehension.
- Parameters:
score (float) – A score from 0-100 indicating how clear and understandable the response is. Higher scores indicate better clarity of the response.
- class trustwise.sdk.metrics.v4.types.FormalityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for formality evaluation.
Formality measures the level of formality in written text, distinguishing between casual, conversational, and formal writing styles.
- Parameters:
- class trustwise.sdk.metrics.v4.types.FormalityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for formality evaluation.
Formality scores indicate the level of formality in the text. Higher scores indicate more formal writing, lower scores indicate more casual writing.
- Parameters:
score (float) – Formality score (0-100). The Formality metric judges how formal a piece of text is. A higher score indicates the text is more formal, and a lower score indicates the text is more informal.
- class trustwise.sdk.metrics.v4.types.SimplicityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for simplicity evaluation.
Simplicity measures how easy it is to understand the words in a text. It gives higher scores to writing that uses common, everyday words instead of special terms or complicated words. Simplicity looks at the words you choose, not how you put them together in sentences.
- Parameters:
- class trustwise.sdk.metrics.v4.types.SimplicityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for simplicity evaluation.
Simplicity measures how easy it is to understand the words in a text. It gives higher scores to writing that uses common, everyday words instead of special terms or complicated words. Simplicity looks at the words you choose, not how you put them together in sentences.
- Parameters:
score (float) – Simplicity score (0-100). A higher score indicates a simpler response, with fewer words and simpler language which is easier to understand.
- class trustwise.sdk.metrics.v4.types.ToneRequest(**data)[source]
Bases:
SDKRequestModelRequest type for tone evaluation.
Tone evaluation identifies the emotional tone and sentiment expressed in the text across multiple emotional categories.
- Parameters:
- class trustwise.sdk.metrics.v4.types.ToneResponse(**data)[source]
Bases:
SDKResponseModelResponse type for tone evaluation.
Tone analysis identifies the emotional tones present in the text, with confidence scores for each detected tone category.
- Parameters:
scores (list) – List of detected tones with confidence scores (0-100). Higher scores indicate stronger presence of that tone.
-
scores:
list[ObjectStyleScore]
- class trustwise.sdk.metrics.v4.types.ToxicityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for toxicity evaluation.
Toxicity evaluation identifies harmful or inappropriate content across multiple categories like threats, insults, and hate speech.
- Parameters:
- class trustwise.sdk.metrics.v4.types.ToxicityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for toxicity evaluation.
Toxicity scores indicate the presence of harmful content across different categories. Higher scores indicate more toxic content, so lower scores are preferred.
- Parameters:
-
scores:
list[ObjectStyleScore]
- class trustwise.sdk.metrics.v4.types.PIIEntity(**data)[source]
Bases:
SDKBaseModelA detected piece of personally identifiable information.
PII entities represent sensitive information that has been identified in text, including its location and category.
- Parameters:
- class trustwise.sdk.metrics.v4.types.PIIRequest(**data)[source]
Bases:
SDKRequestModelRequest type for PII detection.
PII detection identifies personally identifiable information in text based on customizable allowlists and blocklists.
- Parameters:
metadata (dict[str, object] | None) – Optional metadata for the request that will be included in the response.
text (str) – The text to evaluate for PII detection.
allowlist (list[str] | None) – (Optional) List of allowed PII strings or regex patterns.
blocklist (list[str] | None) – (Optional) List of blocked PII strings or regex patterns.
categories (list[str] | None) – (Optional) List of PII categories to evaluate.
- class trustwise.sdk.metrics.v4.types.PIIResponse(**data)[source]
Bases:
SDKResponseModelResponse type for PII detection.
Contains all detected PII entities with their locations and categories for further processing or filtering.
- Parameters:
pii (list) – List of detected PII occurrences.
- class trustwise.sdk.metrics.v4.types.HelpfulnessRequest(**data)[source]
Bases:
SDKRequestModelRequest type for helpfulness evaluation.
Helpfulness measures how useful a given text is. It gives higher scores to texts that fully explain a topic. Helpful responses provide clear, complete information.
- Parameters:
- class trustwise.sdk.metrics.v4.types.HelpfulnessResponse(**data)[source]
Bases:
SDKResponseModelResponse type for helpfulness evaluation.
Helpfulness measureshow useful a given text is. It gives higher scores to texts that fully explain a topic. Helpful responses provide clear, complete information.
- Parameters:
score (float) – A score from 0-100 indicating how helpful the response is. Higher scores indicate better helpfulness of the response.
- class trustwise.sdk.metrics.v4.types.SensitivityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for sensitivity evaluation.
- Parameters:
- class trustwise.sdk.metrics.v4.types.SensitivityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for sensitivity evaluation.
Sensitivity evaluation checks for the presence of specific topics or themes in the text, providing scores for each requested topic.
- Parameters:
scores (list) – List of sensitivity scores for each requested topic (0-100). A high score for a topic indicates that the topic is present in the text.
-
scores:
list[ObjectStyleScore]
- class trustwise.sdk.metrics.v4.types.RefusalRequest(**data)[source]
Bases:
SDKRequestModelRequest type for refusal evaluation.
Refusal measures the likelihood that a response is a refusal to answer or comply with the query. This metric helps identify when AI systems decline to provide information or perform tasks.
- Parameters:
- class trustwise.sdk.metrics.v4.types.RefusalResponse(**data)[source]
Bases:
SDKResponseModelResponse type for refusal evaluation.
Refusal scores indicate the likelihood that the response is a refusal to answer or comply. Higher scores indicate stronger refusal behavior.
- Parameters:
score (float) – Refusal score (0-100). A higher score indicates a higher degree (firmness) of refusal.
- class trustwise.sdk.metrics.v4.types.CompletionRequest(**data)[source]
Bases:
SDKRequestModelRequest type for completion evaluation.
Completion measures how well the response completes or follows the query’s instruction.
- Parameters:
- class trustwise.sdk.metrics.v4.types.CompletionResponse(**data)[source]
Bases:
SDKResponseModelResponse type for completion evaluation.
Completion score indicates how well the response completes the query (0-100).
- Parameters:
score (float) – Completion score (0-100). A higher score indicates a better completion of the query.
- class trustwise.sdk.metrics.v4.types.AdherenceRequest(**data)[source]
Bases:
SDKRequestModelRequest type for adherence evaluation.
Adherence evaluation measures how well the response follows a given policy or instruction.
- Parameters:
- class trustwise.sdk.metrics.v4.types.AdherenceResponse(**data)[source]
Bases:
SDKResponseModelResponse type for adherence evaluation.
Adherence score indicates how well the response follows the policy (0-100).
- Parameters:
score (float) – Adherence score (0-100). A higher score indicates better adherence to the policy.
- class trustwise.sdk.metrics.v4.types.StabilityRequest(**data)[source]
Bases:
SDKRequestModelRequest type for stability evaluation.
Stability evaluation measures the consistency of responses to the same prompt.
- Parameters:
- class trustwise.sdk.metrics.v4.types.StabilityResponse(**data)[source]
Bases:
SDKResponseModelResponse type for stability evaluation.
Stability measures how consistent the responses are. A higher score indicates greater consistency among the responses.
- Parameters:
- class trustwise.sdk.metrics.v4.types.CarbonValue(**data)[source]
Bases:
SDKBaseModelA carbon value with unit.
- Parameters:
- class trustwise.sdk.metrics.v4.types.CarbonComponent(**data)[source]
Bases:
SDKBaseModelA carbon component breakdown.
- Parameters:
component (str) – The component name (e.g., ‘operational_gpu’, ‘operational_cpu’, ‘embodied_cpu’).
carbon (CarbonValue) – The carbon value for this component.
-
carbon:
CarbonValue
- class trustwise.sdk.metrics.v4.types.CarbonRequest(**data)[source]
Bases:
SDKRequestModelRequest type for carbon evaluation.
Carbon evaluation measures the carbon footprint of AI operations based on provider, instance type, region, and latency.
- class trustwise.sdk.metrics.v4.types.CarbonResponse(**data)[source]
Bases:
SDKResponseModelResponse type for carbon evaluation.
Carbon evaluation provides the total carbon footprint and breakdown by component (operational GPU, operational CPU, embodied CPU).
- Parameters:
carbon (CarbonValue) – The total carbon footprint.
components (list) – Breakdown of carbon footprint by component.
-
carbon:
CarbonValue
-
components:
list[CarbonComponent]
V3 Types (Legacy)
Note
V3 types are deprecated and only include cost-related types and common SDK types. Use V4 types for all new development.
- class trustwise.sdk.types.SDKBaseModel(**data)[source]
Bases:
BaseModelBase model for all SDK types with common functionality.
- to_json(**kwargs)[source]
Return a JSON string representation of the model. Ensures valid JSON output regardless of Pydantic version. Always excludes None fields by default.
- classmethod validate_score_range(v, min_value, max_value, label)[source]
Validate that a score or list/dict of scores falls within the specified range.
- static format_validation_error(model_cls, validation_error)[source]
Format a Pydantic ValidationError into a user-friendly error message using field types and descriptions. Distinguishes between missing and invalid arguments, and sets the error prefix accordingly.
- _get_field_description(field_name, field_info=None)[source]
Get the description for a field from its field info.
- _format_output(indent=0)[source]
Format the model’s output with proper indentation and descriptions. Used by both __repr__ and __str__ to ensure consistent output.
- _format_nested_dict(data, model_class=None, indent=0)[source]
Format a nested dictionary with field descriptions. If model_class is provided, it will be used to look up field descriptions.
- _repr_mimebundle_(include=None, exclude=None)[source]
Return multiple representations of the object. This is the preferred way to provide multiple representations in Jupyter.
- _format_html(indent_level=0)[source]
Format the model’s output as HTML with proper styling. Used by __repr_html__ to create a pretty representation in Jupyter notebooks.
- _get_value_css_class(value)[source]
Return the appropriate CSS class for a value based on its type.
- class trustwise.sdk.types.SDKRequestModel(**data)[source]
Bases:
SDKBaseModelBase model for all SDK request types.
- class trustwise.sdk.types.SDKResponseModel(**data)[source]
Bases:
SDKBaseModelBase model for all SDK response types.
- class trustwise.sdk.types.CostResponseV3(**data)[source]
Bases:
SDKResponseModelResponse type for cost evaluation.
- Parameters:
- class trustwise.sdk.types.CostRequestV3(**data)[source]
Bases:
SDKRequestModelRequest type for cost evaluation.
- Parameters:
metadata (dict[str, object] | None) – Optional metadata for the request that will be included in the response.
model_name (str) – Model name must be a non-empty string.
model_type (Literal) – Model type must be ‘LLM’ or ‘Reranker’.
model_provider (str) – Model provider must be a non-empty string. Should be one of [‘openai’, ‘togetherai’, ‘huggingface’, ‘nvidia’, ‘azure’]
number_of_queries (int) – Number of queries must be > 0.
total_prompt_tokens (int) – (Optional)Total number of prompt tokens (must be > 0).
total_completion_tokens (int) – (Optional)Total number of completion tokens (must be > 0).
total_tokens (Optional[Annotated]) – (Optional) Total tokens (optional, must be > 0 if provided).
instance_type (str | None) – (Optional) Instance type required for Hugging Face models.
average_latency (Optional[Annotated]) – (Optional) Average latency in milliseconds, must be > 0 if provided.
- class trustwise.sdk.types.GuardrailResponse(**data)[source]
Bases:
SDKResponseModelResponse type for guardrail evaluation.
- Parameters:
Guardrails
- class trustwise.sdk.guardrails.Guardrail(trustwise_client, thresholds, *, block_on_failure=False, callbacks=None)[source]
Bases:
objectGuardrail system for Trustwise API responses.
Warning
This feature is currently in beta. The API and functionality may change in future releases.
- Parameters:
- __init__(trustwise_client, thresholds, *, block_on_failure=False, callbacks=None)[source]
Initialize the guardrail system.
- Parameters:
trustwise_client (
TrustwiseSDK) – The Trustwise client instance.thresholds (
dict[str,float]) – Dictionary of metric names and their threshold values (0-100).block_on_failure (
bool) – Whether to block responses that fail any metric.callbacks (
dict[str,Callable] |None) – Optional callbacks for metric evaluation results.
- Raises:
ValueError – If thresholds are invalid.
- Return type:
None
- _get_metric_kwargs(metric, query, response, context)[source]
Get keyword arguments for a given metric.
The SDK provides a guardrail system to enforce safety and alignment metrics thresholds. The guardrail system can be used to:
Set thresholds for multiple metrics
Block responses that fail metric checks
Execute callbacks when metrics are evaluated
Check for PII content with custom allowlists and blocklists
Example usage:
trustwise = TrustwiseSDK(config)
guardrail = trustwise.guardrails(
thresholds={
"faithfulness": 0.8,
"answer_relevancy": 0.7,
},
block_on_failure=True
)
evaluation = guardrail.evaluate(
query="What is the capital of France?",
response="The capital of France is Paris.",
context=[{"chunk_text": "Paris is the capital of France.", "chunk_id": "doc:idx:1"}]
)
# Pythonic access
print(evaluation.passed)
print(evaluation.results['faithfulness']['result'].score)
# Serialization
print(evaluation.to_dict())
print(evaluation.to_json(indent=2))
The guardrail system returns a trustwise.sdk.types.GuardrailResponse