Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Vertex AI] Refactor generateContentStream to fix Swift 6 warning #14504

Merged
merged 4 commits into from
Mar 5, 2025

Conversation

paulb777
Copy link
Member

@paulb777 paulb777 commented Feb 26, 2025

Refactored GenerativeModel.generateContentStream(...) to use init(_:bufferingPolicy:_:) (AsyncStream.Continuation approach) instead of init(unfolding:) to resolve the Swift 6 warning "Capture of responseIterator with non-Sendable type AsyncThrowingStream<GenerateContentResponse, Error>" discussed in #14503.

#no-changelog

google-labs-jules bot and others added 2 commits February 26, 2025 22:49
The  function in  was refactored to use  instead of returning an iterator.
The  file was also modified to directly return the  and use it with a  loop.

This change allows for more idiomatic use of the stream, improving the code's readability and maintainability.
Copy link

@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.

Hello @paulb777, 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!

Summary of Changes

This pull request addresses a Swift 6 warning and aims to improve the readability and maintainability of the code. The core change involves refactoring the generateContentStream function in GenerativeModel.swift to use an AsyncThrowingStream with a for try await loop instead of returning an iterator. This allows for more idiomatic stream handling. Additionally, a similar change was made to GenerativeAIService.swift to handle the stream using stream.lines and a for try await loop.

Highlights

  • Refactor: The generateContentStream function in GenerativeModel.swift was refactored to use AsyncThrowingStream and a for try await loop for better stream handling.
  • Swift 6 Warning: This pull request fixes a Swift 6 warning, as discussed in issue [VertexAI] Add Swift 6 testing to CI #14503.
  • Idiomatic Stream Usage: The changes promote more idiomatic use of streams, enhancing code readability and maintainability.

Changelog

Click here to see the changelog
  • FirebaseVertexAI/Sources/GenerativeAIService.swift
    • Refactored the stream processing logic to use stream.lines and a for try await loop.
    • The code now iterates through the lines of the stream, parses JSON data, and yields content to the continuation.
    • Error handling is included within the loop to finish the continuation with an error if parsing fails.
    • The continuation.finish(throwing: nil) is now called inside the loop and after the loop.
  • FirebaseVertexAI/Sources/GenerativeModel.swift
    • Refactored generateContentStream to return an AsyncThrowingStream directly.
    • The stream is now processed within a Task using a for try await loop.
    • Error handling and prompt feedback checks are performed within the loop.
    • The continuation.yield(response) is called inside the loop to provide valid content.
    • The continuation.finish(throwing: nil) is now called after the loop.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 is currently in preview and 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 to provide feedback.

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.


Did you know?

The concept of streams in programming dates back to the early days of Unix, where they were used to connect different programs together.

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

@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

The pull request refactors the generateContentStream function to address a Swift 6 warning and improves code readability. The changes are generally well-structured and address the issue described in the pull request description. The use of AsyncThrowingStream and await makes the code more modern and easier to follow.

Summary of Findings

  • Error Handling in Streaming: The error handling within the stream.lines loop in GenerativeAIService.swift could be improved to ensure that all potential errors are properly handled and propagated.
  • Redundant finish(throwing: nil) calls: The finish(throwing: nil) calls in GenerativeAIService.swift and GenerativeModel.swift appear to be redundant and could be simplified.
  • Task Management in GenerativeModel.swift: Consider adding error handling within the Task block in GenerativeModel.swift to ensure that any unexpected errors are caught and handled appropriately.

Assessment

The pull request refactors the generateContentStream function to address a Swift 6 warning and improves code readability. The changes appear to be well-structured and address the issue described in the pull request description. However, there are a few areas where the code could be improved for clarity and error handling. I recommend addressing these comments before merging, and users should have others review and approve this code before merging.

@ncooke3
Copy link
Member

ncooke3 commented Mar 5, 2025

Screenshot 2025-03-04 at 7 12 06 PM

@ncooke3 ncooke3 marked this pull request as ready for review March 5, 2025 00:22
@ncooke3 ncooke3 requested a review from andrewheard March 5, 2025 14:15
@andrewheard andrewheard changed the title Refactor generateContentStream to fix Swift 6 warning [Vertex AI] Refactor generateContentStream to fix Swift 6 warning Mar 5, 2025
Copy link
Contributor

@andrewheard andrewheard left a comment

Choose a reason for hiding this comment

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

LGTM to me now. (I just pushed a minor change to rename responseIterator to responseStream for clarity and updated the PR name/description)

One thing I'm not certain about is whether this affects the ability for the Task to get cancelled if the caller stops reading with for try await but I think getting the Swift 6 build in good shape is more important.

@paulb777 paulb777 dismissed gemini-code-assist[bot]’s stale review March 5, 2025 18:49

Not relevant after Nick's changes

@paulb777 paulb777 merged commit 2583fef into main Mar 5, 2025
39 of 40 checks passed
@paulb777 paulb777 deleted the fix-generate-content-stream branch March 5, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants