Langfuse JS/TS SDKs
    Preparing search index...

    Class ChatPromptClient

    Client for working with chat-based prompts.

    Provides methods to compile chat prompts with variable substitution and placeholder resolution, and convert them to LangChain-compatible formats.

    Hierarchy

    • BasePromptClient
      • ChatPromptClient
    Index

    Constructors

    • Creates a new ChatPromptClient instance.

      Parameters

      • prompt: Chat

        The chat prompt data

      • isFallback: boolean = false

        Whether this is fallback content

      Returns ChatPromptClient

    Properties

    commitMessage: undefined | null | string

    Optional commit message for the prompt version

    config: unknown

    Configuration object associated with the prompt

    isFallback: boolean

    Whether this prompt client is using fallback content

    labels: string[]

    Labels associated with the prompt

    name: string

    The name of the prompt

    prompt: ChatMessageWithPlaceholders[]

    The chat messages that make up the prompt

    promptResponse: Chat

    The original prompt response from the API

    tags: string[]

    Tags associated with the prompt

    type: "chat" | "text"

    The type of prompt ("text" or "chat")

    version: number

    The version number of the prompt

    Methods

    • Parameters

      • content: string

      Returns string

    • Compiles the chat prompt by replacing placeholders and variables.

      First resolves placeholders with provided values, then applies variable substitution to message content using Mustache templating. Unresolved placeholders remain as placeholder objects in the output.

      Parameters

      • Optionalvariables: Record<string, string>

        Key-value pairs for Mustache variable substitution in message content

      • Optionalplaceholders: Record<string, any>

        Key-value pairs where keys are placeholder names and values are ChatMessage arrays

      Returns any[]

      Array of ChatMessage objects and unresolved placeholder objects

      const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
      const compiled = prompt.compile(
      { user_name: "Alice" },
      { examples: [{ role: "user", content: "Hello" }, { role: "assistant", content: "Hi!" }] }
      );
    • Escapes every curly brace that is part of a JSON object by doubling it.

      A curly brace is considered “JSON-related” when, after skipping any immediate whitespace, the next non-whitespace character is a single (') or double (") quote.

      Braces that are already doubled (e.g. {{variable}} placeholders) are left untouched.

      Parameters

      • text: string

        Input string that may contain JSON snippets.

      Returns string

      The string with JSON-related braces doubled.

    • Converts the prompt to LangChain ChatPromptTemplate format.

      Resolves placeholders with provided values and converts unresolved ones to LangChain MessagesPlaceholder objects. Transforms variables from {{var}} to {var} format without rendering them.

      Parameters

      • Optionaloptions: { placeholders?: Record<string, any> }

        Configuration object

        • Optionalplaceholders?: Record<string, any>

          Key-value pairs for placeholder resolution

      Returns any[]

      Array of ChatMessage objects and LangChain MessagesPlaceholder objects

      const prompt = await langfuse.prompt.get("conversation", { type: "chat" });
      const langchainFormat = prompt.getLangchainPrompt({
      placeholders: { examples: [{ role: "user", content: "Hello" }] }
      });
    • Serializes the prompt client to JSON.

      Returns string

      JSON string representation of the prompt