Skip to main content

PromptTestConfig

Struct PromptTestConfig 

Source
pub struct PromptTestConfig {
Show 21 fields pub inherits: Option<String>, pub name: Option<String>, pub prompt: Option<String>, pub messages: Option<Vec<MessageParam>>, pub system: Option<String>, pub model: Option<String>, pub max_tokens: Option<u32>, pub temperature: Option<f32>, pub top_p: Option<f32>, pub top_k: Option<u32>, pub stop_sequences: Option<Vec<String>>, pub tools: Option<Vec<ToolUnionParam>>, pub tool_choice: Option<ToolChoice>, pub expected_contains: Option<Vec<String>>, pub expected_not_contains: Option<Vec<String>>, pub min_response_length: Option<usize>, pub max_response_length: Option<usize>, pub expected_tool_calls: Option<Vec<String>>, pub expect_error: Option<bool>, pub expected_error_message: Option<String>, pub output_format: Option<OutputFormat>,
}
Expand description

Configuration for a prompt test with support for inheritance and file references.

This structure represents a complete prompt test configuration that can be loaded from YAML files, inherit from base configurations, and automatically resolve file references for content.

§File Reference Resolution

When loading from files, the following fields support automatic file resolution:

  • prompt: If the value is a relative path ending with “prompt.yaml”, the file content is loaded
  • system: If the value is a relative path ending with “system.md”, the file content is loaded

§Security Considerations

File resolution is restricted for security:

  • Only relative paths are resolved (absolute paths remain as literal strings)
  • Only specific filenames (“prompt.yaml”, “system.md”) trigger file loading
  • Files are resolved relative to the configuration file’s directory

§Examples

§Creating a basic configuration:

let config = PromptTestConfig::new("What is the capital of France?")
    .with_model("claude-haiku-4-5")
    .expect_contains("Paris");

§Loading from file with automatic file references:

If test.yaml contains:

name: "Geography Test"
prompt: "prompt.yaml"    # Content loaded from prompt.yaml
system: "system.md"      # Content loaded from system.md
model: "claude-haiku-4-5"
expected_contains:
  - "capital"

Then:

let config = PromptTestConfig::from_file("test.yaml")?;
// config.prompt now contains the contents of prompt.yaml
// config.system now contains the contents of system.md

Fields§

§inherits: Option<String>

Base configuration to inherit from (filename within prompts directory).

§name: Option<String>

Name of the test (optional).

§prompt: Option<String>

The prompt text to send (for single-turn conversations).

When loading from files, if this field contains a relative path ending with “prompt.yaml”, the content will be automatically loaded from that file.

§messages: Option<Vec<MessageParam>>

Multi-turn conversation messages (alternative to prompt).

§system: Option<String>

Optional system prompt.

When loading from files, if this field contains a relative path ending with “system.md”, the content will be automatically loaded from that file.

§model: Option<String>

Model to use for testing.

§max_tokens: Option<u32>

Maximum tokens to generate.

§temperature: Option<f32>

Temperature setting (0.0 to 1.0).

§top_p: Option<f32>

Top-p setting (0.0 to 1.0).

§top_k: Option<u32>

Top-k setting.

§stop_sequences: Option<Vec<String>>

Stop sequences.

§tools: Option<Vec<ToolUnionParam>>

Tools available for the conversation.

§tool_choice: Option<ToolChoice>

How the model should use the provided tools.

§expected_contains: Option<Vec<String>>

Expected content that should appear in the response.

§expected_not_contains: Option<Vec<String>>

Expected content that should NOT appear in the response.

§min_response_length: Option<usize>

Minimum expected response length.

§max_response_length: Option<usize>

Maximum expected response length.

§expected_tool_calls: Option<Vec<String>>

Expected tool calls (name of tools that should be called).

§expect_error: Option<bool>

Whether this test is expected to fail with an API error.

§expected_error_message: Option<String>

Expected error message (substring match).

§output_format: Option<OutputFormat>

Output format for structured outputs.

When set, constrains Claude’s response to follow a specific JSON schema, ensuring valid, parseable output for downstream processing.

Implementations§

Source§

impl PromptTestConfig

Source

pub fn new(prompt: impl Into<String>) -> Self

Create a new prompt test configuration with just a prompt.

§Examples
let config = PromptTestConfig::new("What is the capital of France?");
assert_eq!(config.prompt, Some("What is the capital of France?".to_string()));
Source

pub fn new_conversation(messages: Vec<MessageParam>) -> Self

Create a new multi-turn conversation test.

§Examples
let messages = vec![
    MessageParam::user("Hello"),
    MessageParam::assistant("Hi there! How can I help you?"),
    MessageParam::user("What's the weather like?"),
];
let config = PromptTestConfig::new_conversation(messages);
assert!(config.messages.is_some());
assert!(config.prompt.is_none());
Source

pub fn with_name(self, name: impl Into<String>) -> Self

Set the test name.

Source

pub fn with_system(self, system: impl Into<String>) -> Self

Set the system prompt.

Source

pub fn with_model(self, model: impl Into<String>) -> Self

Set the model to use.

Source

pub fn with_max_tokens(self, max_tokens: u32) -> Self

Set the maximum tokens.

Source

pub fn with_temperature(self, temperature: f32) -> Self

Set the temperature.

Source

pub fn expect_contains(self, content: impl Into<String>) -> Self

Add expected content that should appear in the response.

Source

pub fn expect_not_contains(self, content: impl Into<String>) -> Self

Add content that should NOT appear in the response.

Source

pub fn with_min_length(self, min_length: usize) -> Self

Set minimum expected response length.

Source

pub fn with_max_length(self, max_length: usize) -> Self

Set maximum expected response length.

Source

pub fn with_tool(self, tool: ToolUnionParam) -> Self

Add a tool to the configuration.

Source

pub fn with_tool_choice(self, tool_choice: ToolChoice) -> Self

Set the tool choice configuration.

Source

pub fn expect_tool_call(self, tool_name: impl Into<String>) -> Self

Expect a specific tool to be called.

Source

pub fn expect_error(self) -> Self

Expect the API call to fail with an error.

Source

pub fn expect_error_message(self, message: impl Into<String>) -> Self

Expect a specific error message (substring match).

Source

pub fn with_output_format(self, output_format: OutputFormat) -> Self

Set the output format for structured outputs.

When set, constrains Claude’s response to follow a specific JSON schema, ensuring valid, parseable output for downstream processing.

§Examples
let config = PromptTestConfig::new("Extract the person's name and age")
    .with_output_format(OutputFormat::json_schema(json!({
        "type": "object",
        "properties": {
            "name": { "type": "string" },
            "age": { "type": "integer" }
        },
        "required": ["name", "age"],
        "additionalProperties": false
    })));
Source

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn Error>>

Load a prompt test configuration from a YAML file with inheritance and file reference support.

This method provides several key features:

§Configuration Inheritance

Supports configuration inheritance via the inherits field, allowing you to build configuration hierarchies. Security restrictions apply: only relative paths are allowed, and parent directory traversal is only permitted for base.yaml files.

§File Reference Resolution

Automatically loads content from external files when:

  • prompt field contains a relative path ending with “prompt.yaml”
  • system field contains a relative path ending with “system.md”

Files are resolved relative to the configuration file’s directory. Absolute paths are treated as literal strings for security reasons.

§Examples
§Basic usage:
let config = PromptTestConfig::from_file("test_config.yaml")?;
println!("Loaded test: {:?}", config.name);
§File references:

If your config file contains:

name: "My Test"
prompt: "prompt.yaml"     # This file will be loaded
system: "system.md"       # This file will be loaded
model: "claude-haiku-4-5"

The content of prompt.yaml and system.md (relative to the config file) will be automatically loaded into the prompt and system fields.

§Inheritance with file references:
inherits: "../base.yaml"   # Inheritance (base.yaml only for parent dirs)
name: "Specialized Test"
prompt: "custom_prompt.yaml"  # File reference
§Errors

Returns an error if:

  • The file cannot be read
  • The YAML is invalid
  • Referenced prompt.yaml or system.md files cannot be read
  • Inheritance files use absolute paths or unsafe traversal
  • Inherited files cannot be found or loaded
Source

pub fn from_file_with_base_dir<P: AsRef<Path>>( path: P, base_dir: Option<&Path>, ) -> Result<Self, Box<dyn Error>>

Load a prompt test configuration from a YAML file with a specific base directory.

This is the core method that handles both configuration inheritance and file reference resolution. The base_dir parameter allows you to override the directory used for resolving relative file paths.

§File Reference Resolution

Files are automatically loaded when:

  • The prompt field contains a relative path ending with “prompt.yaml”
  • The system field contains a relative path ending with “system.md”

Only these specific filenames are resolved for security reasons. Other filenames or absolute paths are treated as literal strings.

§Base Directory Resolution
  • If base_dir is provided, all relative paths are resolved relative to it
  • If base_dir is None, paths are resolved relative to the config file’s directory
§Examples
// Load config with custom base directory
let config = PromptTestConfig::from_file_with_base_dir(
    "config.yaml",
    Some(Path::new("/custom/base/dir"))
)?;
§Security

File resolution is restricted to specific patterns for security:

  • Only relative paths ending with “prompt.yaml” or “system.md” are resolved
  • Absolute paths are treated as literal strings
  • Parent directory traversal in inheritance is only allowed for “base.yaml” files
§Errors

Returns an error if:

  • The configuration file cannot be read
  • Referenced prompt.yaml or system.md files cannot be read
  • The YAML syntax is invalid
  • Inheritance security restrictions are violated
Source

pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>>

Save a prompt test configuration to a YAML file.

Source

pub async fn run(&self, client: &Anthropic) -> Result<PromptTestResult, Error>

Run the prompt test using the provided Anthropic client.

This method executes the prompt against the Anthropic API and validates all configured assertions. It handles both successful responses and API errors gracefully, allowing tests to verify error conditions.

§Examples
let client = Anthropic::new(None)?;
let config = PromptTestConfig::new("Hello, world!")
    .expect_contains("hello")
    .with_min_length(5);

let result = config.run(&client).await?;
assert!(result.api_success);
println!("Response: {}", result.response);
§Errors

Returns a crate::Error if:

  • Neither prompt nor messages is provided
  • Invalid parameter values (e.g., temperature out of range)
  • Other validation errors during request building

Trait Implementations§

Source§

impl Clone for PromptTestConfig

Source§

fn clone(&self) -> PromptTestConfig

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PromptTestConfig

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for PromptTestConfig

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for PromptTestConfig

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,