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

functional api: Add ability to request previous output #3025

Merged
merged 15 commits into from
Jan 16, 2025

Conversation

eyurtsev
Copy link
Contributor

@eyurtsev eyurtsev commented Jan 14, 2025

  1. The inputs into foo do not affect any state behavior
  2. previous always reflects the previous return value from the function
  3. Anything can be returned and that will be the new state for the function on the next iteration
  4. This API is not meant to support reducers in the inputs/state
  from langgraph.func import entrypoint

  states = []

  # In this version reducers do not work
  @entrypoint(checkpointer=MemorySaver())
  def foo(inputs, *, previous: Any) -> Any:
      states.append(previous)
      return {"previous": previous, "current": inputs}

  config = {"configurable": {"thread_id": "1"}}

  foo.invoke({"a": "1"}, config)
  foo.invoke({"a": "2"}, config)
  foo.invoke({"a": "3"}, config)
  assert states == [
      None,
      {"current": {"a": "1"}, "previous": None},
      {"current": {"a": "2"}, "previous": {"current": {"a": "1"}, "previous": None}},
  ]

@nfcampos
Copy link
Contributor

Please don't remove generator support

@eyurtsev
Copy link
Contributor Author

How do you want to deal with the return value In the generator case? Why should we support it?

@eyurtsev eyurtsev marked this pull request as ready for review January 16, 2025 19:00
@eyurtsev eyurtsev changed the title imperative: version 1 functional api: Add ability to request previous output Jan 16, 2025
# Check if original signature has a writer argument with a matching type.
# If not, we'll inject it into the decorator, but not pass it
# to the wrapped function.
if "writer" in original_sig.parameters:
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this changing?

@eyurtsev eyurtsev merged commit ce30965 into main Jan 16, 2025
52 checks passed
@eyurtsev eyurtsev deleted the eugene/expose_previous_state branch January 16, 2025 22:02
@benjamincburns benjamincburns added the functional-api issue or PR relates to the functional API label Jan 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
functional-api issue or PR relates to the functional API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants