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

Correctly implement citation merging for gemini #391

Merged
merged 3 commits into from
Apr 4, 2025
Merged

Conversation

hadley
Copy link
Member

@hadley hadley commented Mar 27, 2025

Fixes #358

@jcheng5 thought you might want to review since you wrote this code originally.

@hadley hadley requested a review from jcheng5 March 27, 2025 19:11
@jcheng5
Copy link
Collaborator

jcheng5 commented Apr 1, 2025

I just realized I have this uncommitted change sitting in my local ellmer repo:

diff --git a/R/provider-gemini.R b/R/provider-gemini.R
index f90507b..be1de8b 100644
--- a/R/provider-gemini.R
+++ b/R/provider-gemini.R
@@ -370,7 +370,15 @@ merge_optional <- function(merge_func) {
 merge_objects <- function(...) {
   spec <- list(...)
   function(left, right, path = NULL) {
+    # This can happen with the finishReason is something other than STOP
+    if (is.null(right)) {
+      return(left)
+    }
+
     # cat(paste(collapse = "", path), "\n")
+    if (!is.list(right)) {
+      message("right is not a list in ", path, "!")
+    }
     stopifnot(is.list(left), is.list(right), all(nzchar(names(spec))))
     mapply(
       names(spec),

Note that it is in merge_objects, not merge_optional, and in the case of citations merge_optional passes through to merge_objects.

  1. Should we add if (is.null(right)) { return(left} } to merge_objects? I think definitely yes, since there's a comment there explaining that it can happen.
  2. Should we also add if (is.null(left)) { return(right) } to merge_objects? Seems like yes? In theory if we don't believe this could happen we should error and check our assumptions when it does happen, but in practice, I feel like the fix will always turn out to be if (is.null(left)) { return(right) } anyway.
  3. If we have 1 and 2, do we still need/want merge_optional to do the early returns it's doing in this PR? I could go either way. In defense of not having the early returns, merge_optional is designed to only protect against the case where the element NEVER appears--it has no opinion on how you merge a null and a non-null value. But again, in practice, are we ever going to not just return the non-null value?

@hadley
Copy link
Member Author

hadley commented Apr 4, 2025

Since you have a better sense of how this should flow, I think we should do 1 and 2 and revert the changes to merge_optional().

@hadley hadley merged commit 8bb9e2f into main Apr 4, 2025
9 checks passed
@hadley hadley deleted the gemini-merge branch April 4, 2025 19:40
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.

Stream merging fails with gemini
2 participants