perf(huggingface_hub): optimize how dict attributes are set#591
Merged
Abhijeet Prasad (AbhiPrasad) merged 1 commit intoJul 17, 2026
Merged
Conversation
…ge, remove _get_field indirection - `_merge_tool_call_delta` no longer runs `_try_to_dict` (Pydantic `model_dump`) on every streaming tool-call delta chunk — `SpanImpl.log_internal` already runs `bt_safe_deep_copy` at send time. Same pattern as #586 (cohere) and #590 (google_genai). - Removed the `_get_field(obj, key)` dict-or-attr helper. HuggingFace's `BaseInferenceType` (parent of every response type and stream chunk on both matrix pins, `latest`=1.23.0 and floor `0.32.0`) inherits from `dict`, so `.get()` works uniformly on every response / chunk / delta. The two places that read from an `InferenceClient` instance now use `getattr(instance, key, None)` directly. - Net: 89 deletions / 60 insertions; also tightens the tool-call-delta loop to `isinstance(entry, dict)` and adds an explicit `if details is None: return {}` guard at the top of `_text_generation_extra_metadata` that was previously implicit via `_get_field`. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Abhijeet Prasad (AbhiPrasad)
approved these changes
Jul 17, 2026
Abhijeet Prasad (AbhiPrasad)
enabled auto-merge (squash)
July 17, 2026 19:04
Abhijeet Prasad (AbhiPrasad)
deleted the
fix/huggingface-hub-drop-eager-serialization
branch
July 17, 2026 19:11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Audit of
py/src/braintrust/integrations/huggingface_hub/against.agents/skills/sdk-integrations/SKILL.md. Two related fixes:_try_to_dictin stream tool-call merge._merge_tool_call_deltawas running_try_to_dict(entry)(Pydanticmodel_dump("python")) on every streaming tool-call delta chunk just to readindex/id/type/functionfields.SpanImpl.log_internalalready runsbt_safe_deep_copyat send time, so the eager conversion was a redundant recursive walk on the hot path. Same pattern as fix(cohere): drop redundant _try_to_dict pass in tool-call iteration #586 (cohere) and fix(google_genai): add provider metadata, route tools to metadata, drop eager serialization #590 (google_genai)._get_field(obj, key)dict-or-attr helper. HuggingFace'sBaseInferenceType(parent of every response type and stream chunk on both matrix pins —latest=1.23.0 and floor=0.32.0) inherits fromdict:.get()works uniformly on every response, chunk, delta, and nested field — the dict-or-attr dispatch was dead cleverness. The two places that read from anInferenceClientinstance now usegetattr(instance, key, None)directly.Net diff: 60 insertions / 89 deletions, one file. Also picked up two small invariants that were previously implicit via
_get_field:isinstance(entry, dict)(tighter thanis None) and drops the correspondingisinstance(incoming_fn, dict)re-check inside._text_generation_extra_metadatagets an explicitif details is None: return {}guard at the top.Everything else was already SKILL-aligned and needed no change:
metadata.provideralways set (kwarg > instance >"huggingface"fallback),metadata.modelset when known.metadata.toolsvia_CHAT_METADATA_KEYSallowlist, not intoinput.context.span_origin.instrumentation.name = "huggingface-hub-auto"via localstart_spanhelper.wrap_huggingface_hubidempotent.Test plan
CI=1 BRAINTRUST_TEST_PACKAGE_VERSION=latest pytest src/braintrust/integrations/huggingface_hub/test_huggingface_hub.py— 24/24 passed (huggingface-hub 1.23.0)CI=1 BRAINTRUST_TEST_PACKAGE_VERSION=0.32.0 pytest src/braintrust/integrations/huggingface_hub/test_huggingface_hub.py— 22 passed, 2 pre-existing skips for text_generation on that pin (_skip_if_text_generation_unavailable)🤖 Generated with Claude Code
Created by abhijeet
Slack thread