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:
object
Main 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:
object
Async 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:
BaseModel
Configuration 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
- class trustwise.sdk.types.SDKBaseModel(**data)[source]
Bases:
BaseModel
Base model for all SDK types.
- 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.
- _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.
- 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.
- _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.Fact(**data)[source]
Bases:
SDKBaseModel
A 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’)
prob (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
- trustwise.sdk.types.Facts
A list of Fact objects representing verified statements from a response.
- class trustwise.sdk.types.FaithfulnessResponse(**data)[source]
Bases:
SDKBaseModel
Response 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.
facts (list) – List of extracted ‘atomic’ facts from the response, each containing the statement, label, associated probability, and location in the text.
- class trustwise.sdk.types.AnswerRelevancyResponse(**data)[source]
Bases:
SDKBaseModel
Response 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.types.ContextNode(**data)[source]
Bases:
SDKBaseModel
A single context node with its metadata.
Context nodes represent individual pieces of information that can be used to evaluate the faithfulness and relevance of responses.
- Parameters:
- trustwise.sdk.types.Context
A list of ContextNode objects representing the context for evaluation.
alias of
list
[ContextNode
]
- class trustwise.sdk.types.ContextRelevancyRequest(**data)[source]
Bases:
SDKBaseModel
Request type for context relevancy evaluation.
- Parameters:
-
context:
list
[ContextNode
]
- class trustwise.sdk.types.ContextRelevancyResponse(**data)[source]
Bases:
SDKBaseModel
Response type for context relevancy evaluation.
- Parameters:
score (float) – Context relevancy score (0-100). Combination of the topics scores, where the higher score indicates a higher relevance of the retrieved context to the query.
topics (list) – List of topics identified from the query.
scores (list) – List of scores for each topic (0-100). Highest score for a topic compared across all context nodes is taken as the topic score.
- class trustwise.sdk.types.SummarizationRequest(**data)[source]
Bases:
SDKBaseModel
Request type for summarization evaluation.
Summarization quality measures how well a response captures the key points from the provided context without adding extraneous information.
- Parameters:
-
context:
list
[ContextNode
]
- class trustwise.sdk.types.SummarizationResponse(**data)[source]
Bases:
SDKBaseModel
Response type for summarization quality evaluation.
Summarization quality measures how well the response captures and represents the key information from the source context.
- Parameters:
score (float) – Summarization score (0-100). A higher score indicates a higher adherence of the response to the context.
- class trustwise.sdk.types.PIIEntity(**data)[source]
Bases:
SDKBaseModel
A 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.types.PIIRequest(**data)[source]
Bases:
SDKBaseModel
Request type for PII detection.
PII detection identifies personally identifiable information in text based on customizable allowlists and blocklists.
- Parameters:
- class trustwise.sdk.types.PIIResponse(**data)[source]
Bases:
SDKBaseModel
Response type for PII detection.
Contains all detected PII entities with their locations and categories for further processing or filtering.
- Parameters:
identified_pii (list) – List of detected PII occurrences.
- class trustwise.sdk.types.PromptInjectionRequest(**data)[source]
Bases:
SDKBaseModel
Request type for prompt injection detection.
Prompt injection detection identifies attempts to manipulate AI systems through specially crafted queries or instructions.
- Parameters:
query (str) – The input query string to evaluate for prompt injection.
- class trustwise.sdk.types.PromptInjectionResponse(**data)[source]
Bases:
SDKBaseModel
Response type for prompt injection detection.
Higher scores indicate a greater likelihood that the query contains prompt injection attempts that could manipulate the AI system.
- Parameters:
score (float) – Prompt injection score (0-100). A higher score indicates a higher likelihood of the query being used as a prompt injection.
- class trustwise.sdk.types.ClarityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for clarity evaluation.
Clarity measures how well-written and understandable a response is, considering factors like sentence structure, vocabulary, and coherence.
- Parameters:
response (str) – The response text to evaluate for clarity.
- class trustwise.sdk.types.ClarityResponse(**data)[source]
Bases:
SDKBaseModel
Response 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.types.HelpfulnessRequest(**data)[source]
Bases:
SDKBaseModel
Request type for helpfulness evaluation.
Helpfulness measures how useful and actionable a response is, considering factors like completeness, accuracy, and practical value.
- Parameters:
response (str) – The response text to evaluate for helpfulness.
- class trustwise.sdk.types.HelpfulnessResponse(**data)[source]
Bases:
SDKBaseModel
Response type for helpfulness evaluation.
Helpfulness scores indicate how useful and actionable the response is. Higher scores suggest the response provides valuable, complete information.
- Parameters:
score (float) – Helpfulness score (0-100). A higher score indicates a higher confidence in the response being useful, detailed, and unambiguous.
- class trustwise.sdk.types.FormalityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for formality evaluation.
Formality measures the level of formality in written text, distinguishing between casual, conversational, and formal writing styles.
- Parameters:
response (str) – The response text to evaluate for formality.
- class trustwise.sdk.types.FormalityResponse(**data)[source]
Bases:
SDKBaseModel
Response 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.
sentences (list) – List of sentences analyzed.
scores (list) – List of scores for each sentence (0-100).
- class trustwise.sdk.types.SimplicityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for simplicity evaluation.
Simplicity measures how easy a response is to understand, considering factors like sentence length, vocabulary complexity, and structure.
- Parameters:
response (str) – The response text to evaluate for simplicity.
- class trustwise.sdk.types.SimplicityResponse(**data)[source]
Bases:
SDKBaseModel
Response type for simplicity evaluation.
Simplicity scores indicate how easy the response is to understand. Higher scores suggest simpler, more accessible language and structure.
- 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.types.SensitivityResponse(**data)[source]
Bases:
SDKBaseModel
Response 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 (dict) – Mapping of user-defined topics to sensitivity score (0-100). A high score for a topic indicates that the topic is present in the text.
- class trustwise.sdk.types.ToxicityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for toxicity evaluation.
Toxicity evaluation identifies harmful or inappropriate content across multiple categories like threats, insults, and hate speech.
- Parameters:
response (str) – The LLM response to evaluate.
- class trustwise.sdk.types.ToxicityResponse(**data)[source]
Bases:
SDKBaseModel
Response 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:
- class trustwise.sdk.types.ToneRequest(**data)[source]
Bases:
SDKBaseModel
Request type for tone evaluation.
Tone evaluation identifies the emotional tone and sentiment expressed in the text across multiple emotional categories.
- Parameters:
response (str) – The response text to evaluate for tone.
- class trustwise.sdk.types.ToneResponse(**data)[source]
Bases:
SDKBaseModel
Response type for tone evaluation.
Tone analysis identifies the emotional tones present in the text, with confidence scores for each detected tone category.
- Parameters:
labels (list) – List of identified tones in the response, ordered by their scores. Supported tones: (amusement, anger, caring, confusion, curiosity, disappointment, disgust, embarrassment, excitement, fear, happiness, nervousness, realization, relief, remorse, sadness, surprise, neutral)
scores (list) – Corresponding confidence scores (0-100) for each identified tone. Higher scores indicate stronger presence of that tone.
- class trustwise.sdk.types.CostResponse(**data)[source]
Bases:
SDKBaseModel
Response type for cost evaluation.
- Parameters:
- class trustwise.sdk.types.CarbonResponse(**data)[source]
Bases:
SDKBaseModel
Response type for carbon emissions evaluation.
- Parameters:
carbon_emitted (float) – The carbon emissions associated with the user specified processor (kg CO2e)
sci_per_api_call (float) – Amount of carbon dioxide (CO2 in KG equivalent) emissions generated per individual LLM call.
sci_per_10k_calls (float) – Cumulative carbon emissions associated with 10,000 LLM calls.
- class trustwise.sdk.types.FaithfulnessRequest(**data)[source]
Bases:
SDKBaseModel
Request type for faithfulness evaluation.
- Parameters:
-
context:
list
[ContextNode
]
- class trustwise.sdk.types.AnswerRelevancyRequest(**data)[source]
Bases:
SDKBaseModel
Request type for answer relevancy evaluation.
- class trustwise.sdk.types.SensitivityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for sensitivity evaluation.
- Parameters:
- class trustwise.sdk.types.CostRequest(**data)[source]
Bases:
SDKBaseModel
Request type for cost evaluation.
- Parameters:
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.CarbonRequest(**data)[source]
Bases:
SDKBaseModel
Request type for carbon evaluation.
- Parameters:
- class trustwise.sdk.types.GuardrailResponse(**data)[source]
Bases:
SDKBaseModel
Response type for guardrail evaluation.
- Parameters:
- class trustwise.sdk.types.StabilityRequest(**data)[source]
Bases:
SDKBaseModel
Request type for stability evaluation.
Stability measures the consistency of responses to the same prompt.
- Parameters:
responses (list) – A list of responses to evaluate for stability. Must contain at least two responses.
- class trustwise.sdk.types.StabilityResponse(**data)[source]
Bases:
SDKBaseModel
Response type for stability evaluation.
Stability measures how consistent the responses are. A higher score indicates greater consistency among the responses.
- Parameters:
- class trustwise.sdk.types.RefusalRequest(**data)[source]
Bases:
SDKBaseModel
Request type for refusal evaluation.
Refusal measures the likelihood that a response is a refusal to answer or comply with the query.
- Parameters:
- class trustwise.sdk.types.RefusalResponse(**data)[source]
Bases:
SDKBaseModel
Response type for refusal evaluation.
Refusal score indicates the likelihood that the response is a refusal (0-100).
- Parameters:
score (int) – Refusal score (0-100). A higher score indicates a higher degree (firmness) of refusal.
- class trustwise.sdk.types.CompletionRequest(**data)[source]
Bases:
SDKBaseModel
Request type for completion evaluation.
Completion measures how well the response completes or follows the query’s instruction.
- Parameters:
- class trustwise.sdk.types.CompletionResponse(**data)[source]
Bases:
SDKBaseModel
Response type for completion evaluation.
Completion score indicates how well the response completes the query (0-100).
- Parameters:
score (int) – Completion score (0-100). A higher score indicates a better completion of the query.
- class trustwise.sdk.types.AdherenceRequest(**data)[source]
Bases:
SDKBaseModel
Request type for adherence evaluation.
Adherence measures how well the response follows a given policy or instruction.
- Parameters:
- class trustwise.sdk.types.AdherenceResponse(**data)[source]
Bases:
SDKBaseModel
Response type for adherence evaluation.
Adherence score indicates how well the response follows the policy (0-100).
- Parameters:
score (int) – Adherence score (0-100). A higher score indicates better adherence to the policy.
Guardrails
- class trustwise.sdk.guardrails.Guardrail(trustwise_client, thresholds, *, block_on_failure=False, callbacks=None)[source]
Bases:
object
Guardrail 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,
"clarity": 0.7
},
block_on_failure=True
)
evaluation = guardrail.evaluate(
query="What is the capital of France?",
response="The capital of France is Paris.",
context=context
)
# 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