Skip to content

[SDK] fix: keep LangChain usage extraction when model metadata is absent - #7861

Open
feiiiiii5 wants to merge 3 commits into
comet-ml:mainfrom
feiiiiii5:fix/usage-survives-missing-model-metadata
Open

[SDK] fix: keep LangChain usage extraction when model metadata is absent#7861
feiiiiii5 wants to merge 3 commits into
comet-ml:mainfrom
feiiiiii5:fix/usage-survives-missing-model-metadata

Conversation

@feiiiiii5

Copy link
Copy Markdown

Details

try_extract_provider_usage_data discards an already-extracted usage payload whenever the model-name or provider lookup raises: the orchestrator catches the exception and returns None, so the span is emitted with no usage/cost even though the tokens were extractable. The lookups assume keys that are not guaranteed by LangChain run dicts (extra, outputs.generations[-1][-1].generation_info).

Change: guard every model-name/provider lookup across the six provider extractors so missing keys resolve to None instead of raising; usage is returned with model=None when model metadata is absent.

Issues

Resolves #7860

Testing

  • New unit tests tests/unit/integrations/langchain/provider_usage_extractors/test_model_name_resilience.py: streaming-shape OpenAI run without generation_info and invoke-shape run without extra both return usage (prompt_tokens=10) with model=None. Pre-fix both fail (usage dropped); post-fix 2/2 pass.
  • pytest tests/unit/llm_usage — 65 passed.
  • ruff check and ruff format clean on all touched files.

Documentation

No docs change: behavior change is internal to usage extraction.

try_extract_provider_usage_data discards an already-extracted usage payload whenever the model-name or provider lookup raises (unguarded run_dict["extra"] / generations[-1][-1]["generation_info"] accesses), so spans lose their usage and cost when model metadata is missing. Guard the lookups so usage survives with model=None.

Resolves comet-ml#7860

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
@github-actions github-actions Bot added python Pull requests that update Python code tests Including test files, or tests related like configuration. Python SDK labels Aug 16, 2026
@feiiiiii5
feiiiiii5 marked this pull request as ready for review August 16, 2026 07:53
@feiiiiii5
feiiiiii5 requested a review from a team as a code owner August 16, 2026 07:53
Follow-up from review: explicit None in extra.metadata or extra.invocation_params still aborted usage extraction (AttributeError on the None result, or is_provider_run returning False when try_get_ls_metadata raised on a missing extra). Normalize non-mapping metadata/invocation_params to absent and guard the shared ls-metadata helper, so usage survives with model=None in every shape.

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
Consolidate the three OpenAI resilience tests into a single
pytest.mark.parametrize case named
test_try_extract_provider_usage_data__missing_model_metadata__returns_usage_with_null_model
and annotate the fixture builders as Dict[str, Any], per review.

Signed-off-by: fei <204683769+feiiiiii5@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

baz: pending Python SDK python Pull requests that update Python code 🟡 size/M tests Including test files, or tests related like configuration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: LangChain usage extractors drop token usage when model metadata is absent

1 participant