Skip to main content

Module llm

Module llm 

Source
Expand description

Fluent LLM-mocking builders for the MockServer Rust client.

This module is an idiomatic Rust port of the Java/Node/Python client LLM builders (org.mockserver.client.Llm / LlmMockBuilder / LlmConversationBuilder / LlmFailoverBuilder / TurnBuilder) and the underlying model classes (Completion, ToolUse, Usage, StreamingPhysics, EmbeddingResponse). The builders produce exactly the same expectation wire JSON the other clients emit, so a mock scripted from Rust is byte-for-byte equivalent (modulo JSON key order, which the server ignores) to one scripted from Java.

The expectation action is carried in the httpLlmResponse field of an expectation (a sibling of httpRequest, scenarioName, scenarioState, newScenarioState, times, timeToLive, httpResponse). Null/unset fields are omitted from the JSON, matching the server’s NON_NULL serialization.

§Example

use mockserver_client::llm::{llm_mock, completion, Provider};

let expectation = llm_mock("/v1/chat/completions")
    .with_provider(Provider::OPENAI)
    .with_model("gpt-4o")
    .responding_with(completion().with_text("Hello!"))
    .build();

assert_eq!(expectation["httpLlmResponse"]["provider"], "OPENAI");

Structs§

Completion
A mocked LLM chat/completion response, provider-agnostic (mirrors org.mockserver.model.Completion).
EmbeddingResponse
A mocked embedding response — vector shape and determinism (mirrors org.mockserver.model.EmbeddingResponse).
IsolationSource
Where to read the per-session isolation key from an inbound request (mirrors org.mockserver.llm.IsolationSource). Encodes as "<kind>:<name>".
LlmConversationBuilder
Builder for multi-turn LLM conversation mocks with scenario advancement (mirrors org.mockserver.client.LlmConversationBuilder).
LlmFailoverBuilder
Builder for provider failover/retry scenarios (mirrors org.mockserver.client.LlmFailoverBuilder).
LlmMockBuilder
Fluent builder for a single LLM mock expectation (mirrors org.mockserver.client.LlmMockBuilder).
Provider
LLM provider names. Serialized on the wire as the upper-case enum name (mirrors org.mockserver.model.Provider).
Role
Parsed-message roles (mirrors org.mockserver.llm.ParsedMessage.Role).
StreamingPhysics
Controls the timing physics of a streamed (SSE) completion (mirrors org.mockserver.model.StreamingPhysics).
ToolUse
A single tool/function call emitted by the assistant (mirrors org.mockserver.model.ToolUse).
TurnBuilder
Sub-builder configuring one turn of a conversation mock (mirrors org.mockserver.client.TurnBuilder).
Usage
Token usage counts for a completion (mirrors org.mockserver.model.Usage).

Enums§

LlmResponseBody
Either a Completion or an EmbeddingResponse, accepted by responding_with builder methods.

Functions§

completion
Factory mirroring Completion.completion().
conversation
Entry point mirroring LlmConversationBuilder.conversation().
cookie
Isolate conversations by a cookie.
default_error_body
The default error body for the given status code (mirrors the Java/Node/Python defaultErrorBody). Exposed for parity/testing.
embedding
Factory mirroring EmbeddingResponse.embedding().
header
Isolate conversations by a request header.
input_tokens
Factory mirroring Usage.inputTokens(n).
jitter
Factory mirroring StreamingPhysics.jitter(j).
llm_failover
Entry point mirroring LlmFailoverBuilder.llmFailover().
llm_mock
Entry point mirroring LlmMockBuilder.llmMock(path).
output_tokens
Factory mirroring Usage.outputTokens(n).
query_parameter
Isolate conversations by a query parameter.
streaming_physics
Factory mirroring StreamingPhysics.streamingPhysics().
tokens_per_second
Factory mirroring StreamingPhysics.tokensPerSecond(n).
tool_use
Factory mirroring ToolUse.toolUse(name).
turn
Entry point mirroring TurnBuilder construction (used with LlmConversationBuilder::turn).
usage
Factory mirroring Usage.usage().