Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Wandb Weave Prompt Classes

From Leeroopedia
Knowledge Sources
Domains Prompt_Engineering, NLP
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete tool for defining parameterized prompt templates provided by the Wandb Weave library.

Description

Weave provides three prompt classes:

  • StringPrompt: Simple text template with {variable} placeholders. format(**kwargs) returns a string.
  • MessagesPrompt: List of message dicts (role/content pairs) with template variables. format(**kwargs) returns a list of formatted message dicts.
  • EasyPrompt: Fluent builder that supports configuration, requirements, binding, row batching, and serialization. Inherits from UserList and Prompt.

All are registered Weave objects that can be published and versioned.

Usage

Use StringPrompt for simple text generation. Use MessagesPrompt for chat-format LLM APIs. Use EasyPrompt for advanced prompt engineering with configuration, validation, and batch operations.

Code Reference

Source Location

  • Repository: wandb/weave
  • File: weave/prompt/prompt.py
  • Lines: L83-545

Signature

class Prompt(Object):
    """Base class for all prompt types."""
    def format(self, **kwargs: Any) -> Any: ...

@register_object
class StringPrompt(Prompt):
    content: str = ""
    def format(self, **kwargs: Any) -> str:
        """Format the template string with provided variables."""

@register_object
class MessagesPrompt(Prompt):
    messages: list[dict] = Field(default_factory=list)
    def format(self, **kwargs: Any) -> list:
        """Format all messages with provided variables."""

@register_object
class EasyPrompt(UserList, Prompt):
    data: list = Field(default_factory=list)
    config: dict = Field(default_factory=dict)
    requirements: dict = Field(default_factory=dict)

    def __init__(
        self,
        content: str | dict | list | None = None,
        *,
        role: str | None = None,
        dedent: bool = False,
        **kwargs: Any,
    ) -> None: ...
    def bind(self, *args: Any, **kwargs: Any) -> "Prompt": ...
    def configure(self, config: dict | None = None, **kwargs: Any) -> "Prompt": ...
    def require(self, param_name: str, **kwargs: Any) -> "Prompt": ...
    def publish(self, name: str | None = None) -> ObjectRef: ...

Import

from weave import StringPrompt, MessagesPrompt, EasyPrompt

I/O Contract

Inputs (StringPrompt)

Name Type Required Description
content str Yes Template string with {variable} placeholders

Outputs (StringPrompt.format)

Name Type Description
return str Formatted string with variables substituted

Inputs (MessagesPrompt)

Name Type Required Description
messages list[dict] Yes List of {"role": str, "content": str} dicts with {variable} placeholders

Outputs (MessagesPrompt.format)

Name Type Description
return list[dict] List of formatted message dicts

Usage Examples

StringPrompt

from weave import StringPrompt

prompt = StringPrompt(content="Translate '{text}' to {language}.")
result = prompt.format(text="Hello", language="French")
# "Translate 'Hello' to French."

MessagesPrompt

from weave import MessagesPrompt

prompt = MessagesPrompt(messages=[
    {"role": "system", "content": "You are a {role}."},
    {"role": "user", "content": "{question}"},
])
messages = prompt.format(role="translator", question="How do you say hello in French?")

EasyPrompt

from weave import EasyPrompt

prompt = EasyPrompt("You are a helpful assistant.", role="system")
prompt.append("Translate '{text}' to {language}.", role="user")
bound = prompt.bind(text="Hello", language="French")
messages = bound()  # Returns formatted messages

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment