Skip to content

Feature Request: Support for pathlib.Path objects in OTLP Exporter attributes #5210

@danilopeixoto

Description

@danilopeixoto

Is your feature request related to a problem?

Currently, passing a pathlib.Path object as an attribute value to a LogRecord (or Span) causes the OTLP Exporter to fail with an Exception: Invalid type <class 'pathlib.PosixPath'>. While I understand the OTLP spec is strict about primitive types, pathlib.Path is a Python standard library type frequently used in logging context (e.g., model paths, log file locations).

Describe the solution you'd like

It would be helpful if the OpenTelemetry Python SDK could automatically coerce pathlib.Path objects to strings during the OTLP encoding process, or provide a standard hook/configuration to handle common non-primitive types without crashing the exporter.

Describe alternatives you've considered

I am currently using a custom structlog processor to manually stringify all Path objects before they reach the OpenTelemetry logging bridge:

def stringify_non_standard_types(event_dict):
    for key, value in event_dict.items():
        if isinstance(value, Path):
            event_dict[key] = str(value)
    return event_dict

While this works, it requires manual configuration for every project and doesn't cover cases where third-party instrumentations might inject Path objects into the context.

Additional Context

Stack trace:

  File ".../opentelemetry/exporter/otlp/proto/common/_internal/__init__.py", line 100, in _encode_value
    raise Exception(f"Invalid type {type(value)} of value {value}")
Exception: Invalid type <class 'pathlib.PosixPath'> of value /Users/user/models/my-model

Would you like to implement a fix?

Yes

Tip

React with 👍 to help prioritize this issue. Please use comments to provide useful context, avoiding +1 or me too, to help us triage it. Learn more here.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    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