Sycophant.Context (sycophant v0.5.0)

Copy Markdown

Public conversation handle for multi-turn LLM interactions.

Context is a builder for conversation state. It holds message history, tools, streaming callbacks, and provider params. Model and response schema are per-call concerns and live outside the context.

Building a conversation

ctx = Context.new()
      |> Context.add(Message.system("You are helpful."))
      |> Context.add(Message.user("Hello!"))

Passing options

opts = Context.to_opts(ctx)

Summary

Functions

Appends one or more messages to the context.

Creates an empty context.

Creates a context from a message list or keyword opts.

Creates a context from messages and keyword opts.

t()

Converts context fields into flat keyword opts for pipeline consumption.

Types

t()

@type t() :: %Sycophant.Context{
  __type__: binary(),
  messages: [
    %Sycophant.Message{
      __type__: binary(),
      content:
        nil
        | binary()
        | [
            %Sycophant.Message.Content.Text{
              __type__: binary(),
              citations:
                nil
                | [
                    %Sycophant.Citation{
                      __type__: binary(),
                      cited_text: nil | binary(),
                      document_index: nil | integer(),
                      document_title: nil | binary(),
                      end_index: nil | integer(),
                      file_id: nil | binary(),
                      source: nil | binary(),
                      start_index: nil | integer(),
                      title: nil | binary(),
                      type:
                        nil
                        | :page_location
                        | :char_location
                        | :content_block_location
                        | :web_search_result_location
                        | :search_result_location,
                      unit: nil | :page | :char | :block,
                      url: nil | binary()
                    }
                  ],
              text: binary(),
              type: binary()
            }
            | %Sycophant.Message.Content.Image{
                __type__: binary(),
                data: nil | binary(),
                media_type: nil | binary(),
                type: binary(),
                url: nil | binary()
              }
            | %Sycophant.Message.Content.Document{
                __type__: binary(),
                citations: boolean(),
                data: nil | binary(),
                file_id: nil | binary(),
                media_type: nil | binary(),
                name: nil | binary(),
                type: binary(),
                url: nil | binary()
              }
            | %Sycophant.Message.Content.Thinking{
                __type__: binary(),
                id: nil | binary(),
                signature: nil | binary(),
                summary: nil | binary(),
                text: nil | binary(),
                type: binary()
              }
            | %Sycophant.Message.Content.RedactedThinking{
                __type__: binary(),
                data: binary(),
                type: binary()
              }
          ],
      metadata: any(),
      role: :user | :assistant | :system | :tool_result,
      tool_call_id: nil | binary(),
      tool_calls:
        nil
        | [
            %Sycophant.ToolCall{
              __type__: binary(),
              arguments: any(),
              id: binary(),
              metadata: any(),
              name: binary()
            }
          ],
      wire_protocol:
        nil
        | :anthropic_messages
        | :openai_completions
        | :openai_responses
        | :bedrock_converse
        | :google_gemini
    }
  ],
  params: any(),
  stream: nil | any(),
  tools: [any()]
}

Functions

add(ctx, messages)

@spec add(t(), Sycophant.Message.t() | [Sycophant.Message.t()]) :: t()

Appends one or more messages to the context.

new()

@spec new() :: t()

Creates an empty context.

new(messages)

@spec new([Sycophant.Message.t()] | keyword()) :: t()

Creates a context from a message list or keyword opts.

new(messages, opts)

@spec new(
  [Sycophant.Message.t()],
  keyword()
) :: t()

Creates a context from messages and keyword opts.

t()

to_opts(ctx)

@spec to_opts(t()) :: keyword()

Converts context fields into flat keyword opts for pipeline consumption.