Coco
Look something up

Core

Look up coco_core messages, tools and injected arguments, runnables and prompts, and the in-memory tracing classes.

Look up coco_core, from the trustcoco-core distribution.


Messages

From coco_core.messages, also importable from coco.messages:

NameDescription
HumanMessage(content, **kwargs)A message from a person. Content is a string or a list of content blocks.
AIMessage(content, *, tool_calls=..., usage_metadata=..., **kwargs)A message from the model, with its tool_calls and usage_metadata.
AIMessageChunkA streamed piece of an AIMessage. Chunks add together with +.
SystemMessage(content, **kwargs)A system instruction.
ToolMessage(content, *, tool_call_id, name=..., status="success" | "error", artifact=...)A tool's result, linked to the model's tool call by tool_call_id.
RemoveMessage(id)Tells the add_messages reducer to delete a message, or all of them with REMOVE_ALL_MESSAGES.
trim_messages(messages, *, max_tokens, token_counter, strategy="last", ...)Trims messages to a token budget and keeps the order valid.

Content blocks include TextContentBlock, ReasoningContentBlock, ImageContentBlock, AudioContentBlock, VideoContentBlock, FileContentBlock, ToolCall and ServerToolCall.

Tools

From coco_core.tools, also importable from coco.tools:

from coco_core.tools import tool, BaseTool, StructuredTool, ToolException, InjectedToolArg, InjectedToolCallId
from pydantic import BaseModel, Field


class SearchInput(BaseModel):
    query: str = Field(description="Search query string")
    limit: int = Field(default=5, description="Maximum results to return")


@tool("search_knowledge_base", args_schema=SearchInput, return_direct=False)
def search_kb(query: str, limit: int = 5) -> str:
    """Search the internal knowledge base for relevant articles."""
    return f"Found {limit} results for '{query}'"

@tool infers the argument schema from the function's signature and docstring, or takes args_schema. Annotate a parameter with InjectedToolArg, InjectedToolCallId, InjectedState, InjectedStore or ToolRuntime to fill it at run time, hidden from the model.

Runnables and prompts

Every runnable, from prompts and models to tools and compiled graphs, has invoke, stream, batch and astream_events, and their async versions.

from coco_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from coco_core.output_parsers import StrOutputParser
from coco_openai import ChatOpenAI

model = ChatOpenAI(model="gpt-4o", temperature=0)

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a technical writer specializing in {domain}."),
        MessagesPlaceholder("history", optional=True),
        ("human", "{question}"),
    ]
)

chain = prompt | model | StrOutputParser()
answer = chain.invoke({"domain": "distributed systems", "question": "Explain consensus."})
NameDescription
RunnableSequenceRuns steps in order. a | b makes one.
RunnableParallelRuns steps on the same input at once.
RunnableLambdaWraps a function as a runnable.
RunnablePassthroughPasses the input through, optionally adding keys.
RunnableBranchPicks a step by condition.
RunnableWithFallbacksTries other runnables when one fails.
RunnableConfigCarries configurable (such as thread_id), tags, metadata, callbacks, max_concurrency and recursion_limit.

Tracing

NameImportDescription
CocoTracercoco_core.tracers.cocoRecords runs in memory and keeps the root run on .latest_run. Never makes network calls.
Run, RunTreecoco_core.tracers._contextA trace span: id, name, run_type, start_time, end_time, inputs, outputs, events, error, tags, extra.
tracing_contextcoco_core.tracers._contextwith tracing_context(enabled=True, project_name="...", tags=[...], metadata={...}): scopes tracing settings.
TracePolicy, omit_payloadcoco.agents.middlewareScrub payloads from middleware spans, globally with configure_trace_policy(...).

See Tracing.

On this page