Skip to content

teff.observability.push

teff.observability.push

Remote trace exporters: push a completed :class:Run to an HTTP endpoint.

None of these exporters need extra dependencies — they use urllib in a background thread, so a slow or failing remote sink never blocks (or fails) the workflow that produced the trace. Errors are retried and then logged.

  • :class:HttpExporter — POST the run JSON to any endpoint (e.g. our teff obs-server ingest, or a generic webhook).
  • :class:LangfuseExporter — adapts a run to the Langfuse public API (POST /api/public/traces) with one span per node and one generation per LLM call.
  • :class:LangsmithExporter — adapts a run to the LangSmith runs API (POST /runs/batch) as a chain with nested node runs and LLM runs.

Classes:

Name Description
HttpExporter

POST each completed run as JSON to a remote ingest endpoint.

LangfuseExporter

Push runs to a Langfuse instance via the public traces API.

LangsmithExporter

Push runs to LangSmith via the /runs/batch endpoint.

HttpExporter

Bases: TraceExporter

POST each completed run as JSON to a remote ingest endpoint.

The body is :meth:Run.to_dict plus created_at (seconds since the epoch) so the receiving side can order runs. Sends are asynchronous: :meth:export returns immediately, :meth:close drains the queue.

Source code in teff/observability/push.py
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
class HttpExporter(TraceExporter):
    """POST each completed run as JSON to a remote ingest endpoint.

    The body is :meth:`Run.to_dict` plus ``created_at`` (seconds since the
    epoch) so the receiving side can order runs.  Sends are asynchronous:
    :meth:`export` returns immediately, :meth:`close` drains the queue.
    """

    def __init__(
        self,
        url: str,
        *,
        headers: dict[str, str] | None = None,
        timeout: float = 10.0,
        retries: int = 3,
        backoff: float = 1.0,
    ):
        self.url = url
        self.headers = dict(headers or {})
        self.timeout = timeout
        self.retries = retries
        self.backoff = backoff
        self._pool = ThreadPoolExecutor(max_workers=1)

    def export(self, run: Run) -> None:
        payload = run.to_dict()
        self._pool.submit(
            _post_json,
            self.url,
            payload,
            headers=self.headers,
            timeout=self.timeout,
            retries=self.retries,
            backoff=self.backoff,
        )

    def close(self) -> None:
        self._pool.shutdown(wait=True)

LangfuseExporter

Bases: TraceExporter

Push runs to a Langfuse instance via the public traces API.

Requires host plus a public_key/secret_key pair (Basic auth). Each node becomes a span observation and every LLM call a generation observation attached to its node.

Source code in teff/observability/push.py
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
class LangfuseExporter(TraceExporter):
    """Push runs to a Langfuse instance via the public traces API.

    Requires ``host`` plus a ``public_key``/``secret_key`` pair (Basic
    auth).  Each node becomes a ``span`` observation and every LLM call a
    ``generation`` observation attached to its node.
    """

    def __init__(
        self,
        host: str,
        public_key: str,
        secret_key: str,
        *,
        timeout: float = 10.0,
        retries: int = 3,
        backoff: float = 1.0,
    ):
        self.url = host.rstrip("/") + "/api/public/traces"
        token = base64.b64encode(f"{public_key}:{secret_key}".encode()).decode()
        self.headers = {"Authorization": f"Basic {token}"}
        self.timeout = timeout
        self.retries = retries
        self.backoff = backoff
        self._pool = ThreadPoolExecutor(max_workers=1)

    def export(self, run: Run) -> None:
        base = _run_base(run)
        observations: list[dict[str, Any]] = []
        for node in run.nodes:
            observations.append(
                {
                    "id": node.node_id,
                    "type": "span",
                    "name": node.node_id,
                    "startTime": _iso_timestamp(base, node.start_ms),
                    "endTime": _iso_timestamp(base, node.end_ms or node.start_ms),
                    "level": "ERROR" if node.status == "error" else "DEFAULT",
                    "metadata": {"node_type": node.node_type, "error": node.error},
                }
            )
            for i, call in enumerate(node.llm_calls):
                observations.append(
                    {
                        "id": f"{node.node_id}:llm:{i}",
                        "type": "generation",
                        "parentObservationId": node.node_id,
                        "name": f"{node.node_id}.llm",
                        "model": call.model,
                        "input": call.messages,
                        "output": call.response,
                        "usage": {
                            "input": call.prompt_tokens,
                            "output": call.completion_tokens,
                        },
                        "startTime": _iso_timestamp(base, node.start_ms),
                        "endTime": _iso_timestamp(base, node.end_ms or node.start_ms),
                        "metadata": {"provider": call.provider, "cached": call.cached},
                    }
                )
        payload = {
            "name": run.name,
            "timestamp": _iso_timestamp(base, 0.0),
            "userId": run.owner,
            "sessionId": run.checkpoint_id,
            "metadata": {
                "status": run.status,
                "total_ms": round(run.total_ms, 3),
                "tags": run.tags,
                "notes": run.notes,
                "topology": run.topology.to_dict(),
            },
            "observations": observations,
        }
        self._pool.submit(
            _post_json,
            self.url,
            payload,
            headers=self.headers,
            timeout=self.timeout,
            retries=self.retries,
            backoff=self.backoff,
        )

    def close(self) -> None:
        self._pool.shutdown(wait=True)

LangsmithExporter

Bases: TraceExporter

Push runs to LangSmith via the /runs/batch endpoint.

Requires an API key (x-api-key header). The run becomes a chain run, each node a child chain run, and each LLM call an llm run. project is passed as extra metadata so the UI can group by project.

Source code in teff/observability/push.py
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
class LangsmithExporter(TraceExporter):
    """Push runs to LangSmith via the ``/runs/batch`` endpoint.

    Requires an API key (``x-api-key`` header).  The run becomes a ``chain``
    run, each node a child ``chain`` run, and each LLM call an ``llm`` run.
    ``project`` is passed as extra metadata so the UI can group by project.
    """

    def __init__(
        self,
        api_url: str,
        api_key: str,
        *,
        project: str | None = None,
        timeout: float = 10.0,
        retries: int = 3,
        backoff: float = 1.0,
    ):
        self.url = api_url.rstrip("/") + "/runs/batch"
        headers = {"x-api-key": api_key}
        if project:
            headers["x-langchain-project"] = project
        self.headers = headers
        self.timeout = timeout
        self.retries = retries
        self.backoff = backoff
        self._pool = ThreadPoolExecutor(max_workers=1)

    def export(self, run: Run) -> None:
        base = _run_base(run)
        run_id = f"teff-{run.name}-{int(base * 1000)}"
        metadata = {
            "teff": True,
            "status": run.status,
            "owner": run.owner,
            "checkpoint_id": run.checkpoint_id,
            "tags": run.tags,
            "notes": run.notes,
            "topology": run.topology.to_dict(),
        }
        runs: list[dict[str, Any]] = [
            {
                "id": run_id,
                "name": run.name,
                "run_type": "chain",
                "inputs": {},
                "outputs": {},
                "start_time": _iso_timestamp(base, 0.0),
                "end_time": _iso_timestamp(base, run.total_ms),
                "extra": {"metadata": metadata},
                "error": None if run.status != "error" else run.notes or "error",
            }
        ]
        for node in run.nodes:
            node_id = f"{run_id}:{node.node_id}"
            runs.append(
                {
                    "id": node_id,
                    "name": node.node_id,
                    "run_type": "chain",
                    "parent_run_id": run_id,
                    "inputs": {},
                    "outputs": {},
                    "start_time": _iso_timestamp(base, node.start_ms),
                    "end_time": _iso_timestamp(base, node.end_ms or node.start_ms),
                    "extra": {
                        "metadata": {
                            "node_type": node.node_type,
                            "error": node.error,
                        }
                    },
                    "error": None if node.status != "error" else node.error,
                }
            )
            for i, call in enumerate(node.llm_calls):
                runs.append(
                    {
                        "id": f"{node_id}:llm:{i}",
                        "name": f"{node.node_id}.llm",
                        "run_type": "llm",
                        "parent_run_id": node_id,
                        "inputs": {"messages": call.messages},
                        "outputs": {"response": call.response},
                        "start_time": _iso_timestamp(base, node.start_ms),
                        "end_time": _iso_timestamp(base, node.end_ms or node.start_ms),
                        "extra": {
                            "metadata": {
                                "provider": call.provider,
                                "model": call.model,
                                "prompt_tokens": call.prompt_tokens,
                                "completion_tokens": call.completion_tokens,
                                "cached": call.cached,
                            }
                        },
                    }
                )
        self._pool.submit(
            _post_json,
            self.url,
            runs,
            headers=self.headers,
            timeout=self.timeout,
            retries=self.retries,
            backoff=self.backoff,
        )

    def close(self) -> None:
        self._pool.shutdown(wait=True)