Skip to content

Conversation

@zarinn3pal
Copy link
Contributor

PR to standardize python samples for feature parity with JS samples.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @zarinn3pal, 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 standardizes the Python samples to achieve feature parity with their JavaScript counterparts. It primarily focuses on refining the xAI plugin by introducing a dedicated configuration class for xAI models, improving the handling of model parameters and tool calls, and streamlining the definition of supported models. These changes enhance the robustness, maintainability, and consistency of the Genkit Python ecosystem.

Highlights

  • XAI Plugin Configuration: Introduced a dedicated XAIConfig class, inheriting from GenerationCommonConfig, to manage xAI-specific model parameters like deferred, reasoning_effort, web_search_options, frequency_penalty, and presence_penalty. This enhances type safety and provides a structured way to pass configuration.
  • Model Information Refactoring: Refactored the model_info.py file to use an XAIGrokVersion StrEnum for supported xAI Grok models and defined individual ModelInfo constants for each model. This improves clarity and maintainability of model definitions.
  • Standardized Generation Usage: Added a new helper function build_generation_usage to consistently construct GenerationUsage objects, reducing redundant code and ensuring accurate token and character usage reporting.
  • Improved Tool Handling: Enhanced the _to_xai_messages method to correctly handle tool types and ensure tool arguments are properly JSON dumped. The _build_params method now robustly handles tool input schemas.
  • Sample Standardization: Updated the deepseek-hello and xai-hello Python samples to align with the new configuration approach and standardize the get_weather tool's functionality, making it simpler and returning a random weather string.
  • Dependency Updates: Added python-multipart as a dependency and specified a minimum version for pypdf in pyproject.toml.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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
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 introduces a dedicated XAIConfig for the xAI plugin, which is a great improvement for handling model-specific parameters. The refactoring to use this config and the new build_generation_usage helper function cleans up the code nicely. The standardization of the Python samples is also a good step towards feature parity with other language samples. I've identified a potential bug in handling tool responses and a small improvement for exception handling. Overall, this is a solid PR with good refactoring and feature enhancement.

Comment on lines 279 to 282
chat_pb2.Message(
role=chat_pb2.MessageRole.ROLE_TOOL,
name=actual_part.tool_response.ref,
content=[chat_pb2.Content(text=str(actual_part.tool_response.output))],
)
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The tool_call_id is missing for tool responses. This is necessary for the model to associate the tool output with the correct tool request. The previous implementation used an incorrect name parameter, and while removing it is correct, the tool_call_id should be added. The ref from tool_response should be used as the tool_call_id.

                        chat_pb2.Message(
                            role=chat_pb2.MessageRole.ROLE_TOOL,
                            tool_call_id=actual_part.tool_response.ref,
                            content=[chat_pb2.Content(text=str(actual_part.tool_response.output))],
                        )

if not isinstance(config, XAIConfig):
try:
config = XAIConfig.model_validate(config)
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Catching a broad Exception can hide configuration issues from the user. It's better to catch the specific pydantic.ValidationError here to make the code more robust and provide clearer feedback on invalid configurations. You'll need to import ValidationError from pydantic. Consider logging a warning when validation fails, so the user is aware that their provided configuration was ignored and default values are being used.

Suggested change
except Exception:
except ValidationError:

def get_weather(input: WeatherInput) -> dict:
"""Get weather information for a location.
def get_weather(input: WeatherInput) -> str:
"""Return a random realistic weather string for a city name.
Copy link
Contributor

Choose a reason for hiding this comment

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

can we please update these flows in other samples as well?

model = XAIModel(model_name=clean_name, client=self._xai_client)
model_info = get_model_info(clean_name)

from genkit.plugins.xai.models import XAIConfig
Copy link
Contributor

Choose a reason for hiding this comment

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

let's move imports to the top of the module


def _create_sample_request() -> GenerateRequest:
"""Create a sample generation request for testing."""
from genkit.plugins.xai.models import XAIConfig
Copy link
Contributor

Choose a reason for hiding this comment

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

let's move imports to the top of the module


@ai.flow()
async def weather_flow(location: Annotated[str, Field(default='San Francisco, CA')] = 'San Francisco, CA') -> str:
async def weather_flow(location: Annotated[str, Field(default='London')] = 'London') -> str:
Copy link
Contributor

Choose a reason for hiding this comment

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

let's fix the location for all weather flows across the samples

@yesudeep yesudeep changed the title feat: Added plugin config, modified samples feat(py/plugins/xai): Added plugin config, modified samples Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants