Skip to content

[FAQ] Module 5: RecursionError with ConsoleSpanExporter on Python 3.14 #332

Description

@dammi01

Course

llm-zoomcamp

Question

Why does the execution script freeze or crash with a "RecursionError: maximum recursion depth exceeded" when initializing OpenTelemetry with ConsoleSpanExporter in Module 5?

Answer

This issue happens when running the code on a local environment with Python 3.14. The pre-release interpreter features updated execution flow and frame management mechanics. The standard OpenTelemetry ConsoleSpanExporter attempts to read raw source code lines via the native linecache module to append metadata to the console printout, which triggers an infinite evaluation loop inside the Python 3.14 codebase.

To bypass this error completely, disable the console text exporter and route your telemetry spans directly to a local SQLite database storage engine by using a custom SpanExporter:

import sqlite3
from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult

class SQLiteSpanExporter(SpanExporter):
    def __init__(self, db_path="traces.db"):
        self.conn = sqlite3.connect(db_path)
        self.conn.execute("""
            CREATE TABLE IF NOT EXISTS spans (
                name TEXT, start_time INTEGER, end_time INTEGER,
                input_tokens INTEGER, output_tokens INTEGER, cost REAL
            )
        """)
        self.conn.commit()

    def export(self, spans):
        for span in spans:
            attrs = dict(span.attributes or {})
            self.conn.execute(
                "INSERT INTO spans VALUES (?, ?, ?, ?, ?, ?)",
                (span.name, span.start_time, span.end_time, 
                 attrs.get("input_tokens", 0), attrs.get("output_tokens", 0), attrs.get("cost", 0.0)),
            )
        self.conn.commit()
        return SpanExportResult.SUCCESS

Initialize your TracerProvider with this database exporter instead of the console processor to solve the execution freeze.

### Checklist

- [x] I have searched existing FAQs and this question is not already answered
- [x] The answer provides accurate, helpful information
- [x] I have included any relevant code examples or links

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions