Skip to content

chore: enhance cache and ObjectRef#296

Merged
k82cn merged 1 commit into
xflops:mainfrom
k82cn:enhance_cache_ref
Jan 14, 2026
Merged

chore: enhance cache and ObjectRef#296
k82cn merged 1 commit into
xflops:mainfrom
k82cn:enhance_cache_ref

Conversation

@k82cn
Copy link
Copy Markdown
Contributor

@k82cn k82cn commented Jan 14, 2026

No description provided.

Signed-off-by: Klaus Ma <klausm@nvidia.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @k82cn, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the object caching mechanism by simplifying the ObjectRef structure and centralizing object serialization and deserialization within the core cache utility functions. The primary goal is to provide a more intuitive and cleaner API for interacting with the object cache, abstracting away the complexities of data serialization from the caller. By removing the DataSource enum and the concept of local object references, all cache operations are standardized to remote interactions, leading to a more consistent and robust caching system.

Highlights

  • Simplified ObjectRef: The ObjectRef class has been streamlined by removing the source and data attributes. It now exclusively represents references to remote cached objects, making the url attribute mandatory.
  • Automated Object Serialization/Deserialization: The put_object, get_object, and update_object functions in cache.py now automatically handle the pickling and unpickling of Python objects. This means users can pass any Python object directly, and the cache functions will manage the serialization process internally.
  • Removal of DataSource Enum: The DataSource enum, which previously distinguished between local and remote data sources, has been removed. All cache operations now implicitly assume interaction with a remote cache endpoint, simplifying the caching logic.
  • Refactored Cache Interactions: Client-side code across client.py, runpy.py, and service.py has been updated to align with the new cache API. This includes removing manual pickling/unpickling steps and simplifying calls to the put_object, get_object, and update_object functions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the caching mechanism and the ObjectRef type. The changes simplify the ObjectRef to only represent remote objects and move the serialization (pickling) logic into the cache helper functions. This is a good improvement for encapsulation and clarity. My review includes suggestions to enhance error handling in the cache functions to make the SDK more robust by using specific FlameError exceptions. I also pointed out and suggested removing a potential issue with log suppression that goes against the repository's guidelines.

Comment on lines +71 to +82
if context._cache_endpoint is None:
raise Exception("Cache endpoint is not configured")

# Serialize the object using pickle
data = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)

with suppress_dependency_logs():
response = httpx.post(f"{context._cache_endpoint}/objects/{session_id}", data=data)
response.raise_for_status()

metadata = ObjectMetadata.model_validate(response.json())
return ObjectRef(source=DataSource.REMOTE, url=metadata.endpoint, data=data, version=metadata.version)


def get_object(de: ObjectRef) -> "ObjectRef":
"""Get an object from the cache."""
if de.source != DataSource.REMOTE:
return de

return ObjectRef(url=metadata.endpoint, version=metadata.version)
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.

high

This function can be improved in a couple of ways:

  1. Error Handling: Raise a specific FlameError instead of a generic Exception. Also, catch potential exceptions from httpx and pydantic and wrap them in FlameError for consistent error handling.
  2. Logging: The use of suppress_dependency_logs goes against the repository's guidelines, as it can interfere with the application's logging configuration. It's better to let the application developer control logging levels.

The suggestion below applies these improvements. You'll also need to import FlameError and FlameErrorCode from .types.

    if context._cache_endpoint is None:
        raise FlameError(FlameErrorCode.INVALID_CONFIG, "Cache endpoint is not configured")

    # Serialize the object using pickle
    data = pickle.dumps(obj, protocol=pickle.HIGHEST_PROTOCOL)

    try:
        response = httpx.post(f"{context._cache_endpoint}/objects/{session_id}", data=data)
        response.raise_for_status()

        metadata = ObjectMetadata.model_validate(response.json())
        return ObjectRef(url=metadata.endpoint, version=metadata.version)
    except httpx.HTTPError as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to put object in cache: {e}") from e
    except Exception as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to process cache response: {e}") from e
References
  1. In a library, avoid modifying the global logging configuration for dependencies. This can cause conflicts with the application's own logging setup. Instead, document how users can configure logging or provide an opt-in utility (like a context manager) for temporary log level adjustments.

Comment on lines 97 to +105
with suppress_dependency_logs():
response = httpx.get(de.url)
response = httpx.get(ref.url)
response.raise_for_status()

obj = Object.model_validate(response.json())

de.data = bytes(obj.data)
de.version = obj.version

return de


def update_object(de: ObjectRef) -> "ObjectRef":
"""Update an object in the cache."""
if de.source != DataSource.REMOTE:
return de

obj = Object(version=de.version, data=list(de.data))
data = bytes(obj.data)

# Deserialize the object using pickle
return pickle.loads(data)
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.

high

This function can be improved with more robust error handling and by adhering to logging guidelines:

  1. Error Handling: Catch potential exceptions from httpx, pydantic, and pickle and wrap them in FlameError.
  2. Logging: The suppress_dependency_logs should be removed to comply with repository guidelines.

The suggestion below applies these improvements.

    try:
        response = httpx.get(ref.url)
        response.raise_for_status()

        obj = Object.model_validate(response.json())
        data = bytes(obj.data)

        # Deserialize the object using pickle
        return pickle.loads(data)
    except httpx.HTTPError as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to get object from cache: {e}") from e
    except pickle.UnpicklingError as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to deserialize object from cache: {e}") from e
    except Exception as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to process cache response: {e}") from e
References
  1. In a library, avoid modifying the global logging configuration for dependencies. This can cause conflicts with the application's own logging setup. Instead, document how users can configure logging or provide an opt-in utility (like a context manager) for temporary log level adjustments.

Comment on lines 127 to +133
with suppress_dependency_logs():
response = httpx.put(de.url, data=data)
response = httpx.put(ref.url, data=data)
response.raise_for_status()

metadata = ObjectMetadata.model_validate(response.json())

de.version = metadata.version

return de
return ObjectRef(url=ref.url, version=metadata.version)
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.

high

This function can be improved with more robust error handling and by adhering to logging guidelines:

  1. Error Handling: Catch potential exceptions from httpx and pydantic and wrap them in FlameError.
  2. Logging: The suppress_dependency_logs should be removed to comply with repository guidelines.

The suggestion below applies these improvements.

    try:
        response = httpx.put(ref.url, data=data)
        response.raise_for_status()

        metadata = ObjectMetadata.model_validate(response.json())

        return ObjectRef(url=ref.url, version=metadata.version)
    except httpx.HTTPError as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to update object in cache: {e}") from e
    except Exception as e:
        raise FlameError(FlameErrorCode.INTERNAL, f"Failed to process cache response: {e}") from e
References
  1. In a library, avoid modifying the global logging configuration for dependencies. This can cause conflicts with the application's own logging setup. Instead, document how users can configure logging or provide an opt-in utility (like a context manager) for temporary log level adjustments.

@k82cn k82cn merged commit 449a82d into xflops:main Jan 14, 2026
3 checks passed
@k82cn k82cn deleted the enhance_cache_ref branch January 14, 2026 08:59
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.

1 participant