trulens.core.schema.prompt¶
trulens.core.schema.prompt
¶
Serializable prompt-management classes.
Prompts are reusable, versioned definitions stored in the configured TruLens
database. A Prompt is the stable identity,
a PromptVersion is one immutable
content-addressed revision of it, and a
PromptLabel is a mutable pointer from
a name such as production to one exact version.
Rendering happens locally and never calls a model.
Attributes¶
VARIABLE_PATTERN
module-attribute
¶
VARIABLE_PATTERN = compile(
"\\{\\{\\s*([A-Za-z_][A-Za-z0-9_]*)\\s*\\}\\}"
)
Pattern for a simple named variable such as {{question}}.
RESERVED_RENDER_KWARGS
module-attribute
¶
RESERVED_RENDER_KWARGS = ('model_overrides', 'strict')
Keyword arguments of the render methods that cannot name a variable.
LATEST_LABEL
module-attribute
¶
LATEST_LABEL = 'latest'
Label moved onto every newly created version.
Classes¶
PromptType
¶
The kind of content a prompt holds.
Fixed at creation time. A prompt cannot change from text to chat or back.
PromptMessage
¶
Bases: SerialModel
One message of a chat prompt.
Attributes¶
content
instance-attribute
¶
Message text, or a list of structured content blocks.
Functions¶
content_variables
¶
Variable names referenced by this message, in order of appearance.
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
InvalidTemplateError
¶
Bases: ValueError
Raised when template content uses unsupported syntax.
VariableError
¶
Bases: ValueError
Raised when render values do not match the declared variables.
Prompt
¶
Bases: SerialModel, Hashable
The stable identity of a reusable prompt.
The identifier is derived from the slug so that renaming the prompt or editing its description does not break existing references.
Attributes¶
prompt_type
instance-attribute
¶
prompt_type: PromptType
Whether versions of this prompt hold text or chat content.
Immutable once the prompt exists.
description
class-attribute
instance-attribute
¶
Free-text description.
tags
class-attribute
instance-attribute
¶
Tags for grouping prompts.
created_at
class-attribute
instance-attribute
¶
When the prompt was first created.
updated_at
class-attribute
instance-attribute
¶
When the prompt metadata last changed.
Functions¶
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
PromptVersion
¶
Bases: SerialModel, Hashable
One immutable revision of a prompt.
The identifier is content-addressed over the prompt identity, the canonical content, the declared variables, the model defaults, and the response format, so creating the same version twice is idempotent.
Attributes¶
parent_version_id
class-attribute
instance-attribute
¶
parent_version_id: Optional[PromptVersionID] = None
The version this one was derived from, when known.
text
class-attribute
instance-attribute
¶
Template string. Set for text prompts only.
messages
class-attribute
instance-attribute
¶
messages: Optional[List[PromptMessage]] = None
Ordered messages. Set for chat prompts only.
variables
class-attribute
instance-attribute
¶
Names of the variables this version declares.
model_defaults
class-attribute
instance-attribute
¶
Provider-neutral model settings persisted with the version.
Never holds credentials.
response_format
class-attribute
instance-attribute
¶
Response-format metadata handed to provider adapters.
change_note
class-attribute
instance-attribute
¶
Why this version was created.
content_hash
class-attribute
instance-attribute
¶
content_hash: str = ''
Hash of the canonical content alone.
created_at
class-attribute
instance-attribute
¶
When the version was created.
created_by
class-attribute
instance-attribute
¶
Who created the version, when the caller supplies it.
Functions¶
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
PromptLabel
¶
Bases: SerialModel
A mutable pointer from a label to one exact version.
staging and production are conventions, not hosted environments.
Attributes¶
version_id
instance-attribute
¶
version_id: PromptVersionID
The exact version the label currently points at.
updated_at
class-attribute
instance-attribute
¶
When the pointer last moved.
Functions¶
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
PromptLabelHistory
¶
Bases: SerialModel
One append-only record of a label movement.
Attributes¶
previous_version_id
class-attribute
instance-attribute
¶
previous_version_id: Optional[PromptVersionID] = None
Where the label pointed before, or None on first assignment.
new_version_id
instance-attribute
¶
new_version_id: PromptVersionID
Where the label points after the move.
moved_by
class-attribute
instance-attribute
¶
Caller label supplied by whoever moved the pointer.
timestamp
class-attribute
instance-attribute
¶
When the move happened.
Functions¶
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
RenderedPrompt
¶
Bases: SerialModel
The local result of rendering one exact version.
No model was called to produce this and no credentials were needed.
Attributes¶
label
class-attribute
instance-attribute
¶
The label that was asked for, when the version came from one.
text
class-attribute
instance-attribute
¶
Rendered text. Set for text prompts only.
messages
class-attribute
instance-attribute
¶
Rendered provider-neutral message dictionaries. Chat prompts only.
model_args
class-attribute
instance-attribute
¶
Persisted model defaults merged with the caller's overrides.
response_format
class-attribute
instance-attribute
¶
Response-format metadata, kept out of model_args on purpose.
Functions¶
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
ResolvedPrompt
¶
Bases: SerialModel
One exact prompt version, plus strict local rendering.
Attributes¶
label
class-attribute
instance-attribute
¶
The label that was requested, when a label was used.
Functions¶
model_args
¶
render
¶
render(
model_overrides: Optional[Dict[str, Any]] = None,
strict: bool = True,
**values: Any
) -> RenderedPrompt
Render this version locally.
| PARAMETER | DESCRIPTION |
|---|---|
model_overrides
|
Model settings that win over the persisted defaults. |
strict
|
When true, values that no declared variable uses are an error rather than being dropped. Missing values are always an error.
TYPE:
|
**values
|
One entry per declared variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RenderedPrompt
|
A RenderedPrompt with |
RenderedPrompt
|
text or ordered message dictionaries, the merged model arguments, |
RenderedPrompt
|
and the response format kept separate. |
| RAISES | DESCRIPTION |
|---|---|
VariableError
|
If the supplied values do not match the declared variables. |
build
¶
build(
model_overrides: Optional[Dict[str, Any]] = None,
strict: bool = True,
**values: Any
) -> List[Dict[str, Any]]
Render a chat version straight to message dictionaries.
| PARAMETER | DESCRIPTION |
|---|---|
model_overrides
|
Model settings that win over the persisted defaults. Accepted so that the signature matches render. |
strict
|
See render.
TYPE:
|
**values
|
One entry per declared variable.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[Dict[str, Any]]
|
Ordered provider-neutral message dictionaries. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If this is a text prompt. |
__repr__
¶
__repr__() -> str
Safe repr that handles circular references.
Pydantic's default __repr__ does not guard against
circular references among model instances, which leads to
RecursionError (see GitHub issue #1862). This override
uses the same formatted_objects context-variable that
__rich_repr__ uses so that already-visited objects are
replaced with a short placeholder instead of recursing
infinitely.
Functions¶
validate_template
¶
Reject template syntax beyond simple named variables.
| PARAMETER | DESCRIPTION |
|---|---|
strings
|
Template strings to check. |
| RAISES | DESCRIPTION |
|---|---|
InvalidTemplateError
|
If a statement tag is present or a |