Skip to content

Support contextual session management#55

Merged
nrfulton merged 3 commits into
generative-computing:mainfrom
elronbandel:contextual-session
Aug 19, 2025
Merged

Support contextual session management#55
nrfulton merged 3 commits into
generative-computing:mainfrom
elronbandel:contextual-session

Conversation

@elronbandel

@elronbandel elronbandel commented Aug 14, 2025

Copy link
Copy Markdown
Contributor

Adds Python contextvars support to enable implicit session usage,
providing a cleaner API while maintaining full backwards compatibility.

Changes

  • New context manager: session() that manages session lifecycle
  • Implicit in-session functions: instruct(), chat(), validate(), query(),
    transform() use current active context session (and fail if called outside the context)
  • Updated generative slots: @Generative functions can now work without explicit
    session parameter
  • Backwards compatible: All existing code continues to work unchanged

Usage

New (cleaner):

from mellea import session, chat, generative

@generative
def add_one(x: int) -> int:
    ...

with session("ollama", "granite"):
     response = chat("Tell me more")
     two = add_one(1)

Still works:

  m = start_session("ollama", "granite")
  result = m.chat("Tell me more")

Also works:

 with session("ollama", "granite") as m:
      result = m.chat("Tell me more")

Signed-off-by: elronbandel <elronbandel@gmail.com>
@nrfulton
nrfulton self-requested a review August 14, 2025 21:49
from typing import Literal

from mellea import generative, start_session
from mellea import generative, session

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I do worry a bit about confusion arising from session vs start_session vs MelleaSession but I'm probably just being paranoid.

@elronbandel elronbandel Aug 17, 2025

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It is confusing. For now I added the context management ability to start_session so we have only start_session which now works also with with start_session():. In the future we can consider having one name that represents both modes, and deprecating this one. Anyways if and when it should be separate PR.

@nrfulton

Copy link
Copy Markdown
Member

I'm inclined to merge; I like it. Adding @HendrikStrobelt as a reviewer to make sure I'm not missing anything obvious.

@elronbandel did you test this with mobject stuff?

…ed objects

Signed-off-by: elronbandel <elronbandel@gmail.com>
@elronbandel

Copy link
Copy Markdown
Contributor Author

@nrfulton I added comprehensive tests, including mified objects.

Signed-off-by: elronbandel <elronbandel@gmail.com>
@elronbandel
elronbandel marked this pull request as ready for review August 17, 2025 14:42
@nrfulton

Copy link
Copy Markdown
Member

LGTM. We need to create some documentation about this, probably in the Context Management section of the tutorial I'll create a new issue for this and merge for now, so that we're not gating a new feature on docs.

@nrfulton
nrfulton merged commit c68ab38 into generative-computing:main Aug 19, 2025
akihikokuroda pushed a commit to akihikokuroda/mellea that referenced this pull request May 27, 2026
* Support contextual session management

Signed-off-by: elronbandel <elronbandel@gmail.com>

* Add comprehensive tests for contextual session functionality and mified objects

Signed-off-by: elronbandel <elronbandel@gmail.com>

* Unify session and start_session

Signed-off-by: elronbandel <elronbandel@gmail.com>

---------

Signed-off-by: elronbandel <elronbandel@gmail.com>
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.

2 participants