Skip to content

Treat logfire.tags passed through logging and loguru as log tags - #2550

Merged
alexmojaki merged 6 commits into
pydantic:mainfrom
Thanatos9404:fix-loguru-tags
Oct 13, 2026
Merged

alexmojaki merged 6 commits into
pydantic:mainfrom
Thanatos9404:fix-loguru-tags

Conversation

@Thanatos9404

@Thanatos9404 Thanatos9404 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #524

What

LogfireLoggingHandler (and therefore the loguru handler) now passes a logfire.tags value from the log record to Logfire.log(tags=...), so it is recorded the same way as logfire.info(..., _tags=[...]).

Why

As noted in #524, logger.info('Hello {name}', name='World', **{'logfire.tags': ['foo', 'bar']}) was expected to work, but the value was treated as an ordinary attribute: it was exported as the JSON string '["foo","bar"]', listed in logfire.json_schema, and overwritten by any tags on the handler's logfire_instance. The same happened with logger.bind(**{'logfire.tags': [...]}) and stdlib logging with extra={'logfire.tags': [...]}.

How

emit() pops logfire.tags from the filled attributes and passes it as tags=, so it is merged with the instance's tags and exported as a string array. A single string is treated as one tag, as the LogfireSpan.tags setter already does; other malformed values behave the same as they do for _tags.

Before / After

logfire.tags for the loguru call above:

  • Before: '["foo","bar"]', plus a logfire.tags entry in logfire.json_schema
  • After: ('foo', 'bar'), no schema entry

With logfire_instance=logfire.with_tags('instance'): before ('instance',), after ('instance', 'foo').

Tests: test_loguru_tags (kwargs and bind), test_loguru_single_string_tag, test_stdlib_logging_tags, test_stdlib_logging_single_string_tag and test_stdlib_logging_tags_merged_with_instance_tags; all fail on main.

The structlog integration calls Logfire.log directly, so it isn't covered here; happy to do that in a follow-up if you'd like.

This change was written with Claude Opus 5.5 (via Claude Code) under my direction, then checked with a separate AI review pass and the relevant tests, ruff and pyright run locally. I'm responsible for it and will follow up on review.

LogfireLoggingHandler passed every extra field to Logfire.log as an
attribute, so `logfire.tags` given via loguru kwargs, `logger.bind()` or
stdlib `extra=` was exported as a JSON string and overwritten by the
instance's own tags. Pass it as `tags=` instead.

Fixes pydantic#524

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 3 files

Reply to a comment to ask cubic a question or push back. It learns from your replies.

View guided diff | Re-trigger cubic

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/integrations
  logging.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki

Copy link
Copy Markdown
Contributor

Thanks!

@alexmojaki
alexmojaki enabled auto-merge (squash) October 13, 2026 13:42
@alexmojaki
alexmojaki merged commit 827bd3d into pydantic:main Oct 13, 2026
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to pass logfire tags via loguru

2 participants