Skip to content

feat(session): add per-call model_options override to instruct() #1452

Closed as not planned

Description

@planetf1

Summary

MelleaSession.instruct() has no way to override model_options (temperature, max_tokens, extra_body, etc.) for a single call. Once a session is constructed with a given OpenAIBackend, all calls use the same backend config. The only way to change any request parameter is to reconstruct the entire session, which discards the HTTP connection pool.

Motivation

We hit this when building an evaluation harness that runs the same MelleaSession across three thinking tiers (NOTHINK / LIGHT / DEEP). Each tier requires a different chat_template_kwargs value in extra_body — but these are request-scoped, not session-scoped. There is no clean way to forward them without tearing down and rebuilding the session for each request.

Current workaround: construct a new MelleaSession (and therefore a new AsyncOpenAI client with a fresh connection pool) per request. This works but:

  • Adds latency and GC pressure under load.
  • Prevents connection reuse.
  • Makes session-level caching (e.g. for adapter loading) useless.

This issue is the complement of feat(backends): add default_extra_body to OpenAIBackend (filed separately). Together they provide a clean two-level override system: construction-time defaults via default_extra_body, and per-call overrides via instruct(model_options=...).

Proposed fix

Add an optional model_options: dict | None = None keyword argument to MelleaSession.instruct(). When provided, merge it with the backend's existing options for that call only (not persisted to the session):

async def instruct(
    self,
    prompt: str,
    requirements: list[Requirement] | None = None,
    *,
    model_options: dict | None = None,   # <-- new
    **kwargs,
) -> SamplingResult:
    ...

Internally, the merged options would be forwarded to the backend for that single call. The session's default options remain unchanged.

Caller side (our use-case):

# Reuse one session; vary thinking tier per call
result = await session.instruct(
    prompt,
    requirements=reqs,
    model_options={"extra_body": {"chat_template_kwargs": {"enable_thinking": True, "thinking_budget": 5000}}},
)

The change is:

  • Additive — model_options=None preserves existing behaviour exactly.
  • Enables session reuse — one MelleaSession per endpoint rather than one per request.
  • Unblocks issue Incorporate example Colab notebooks #4 (MelleaSession not safely reusable across tiers) once default_extra_body is also available.

Environment

  • Mellea v0.6.0
  • Reproducer: any caller that needs to vary extra_body between instruct() calls on the same session.

Activity

  1. added
    enhancement
    on Jul 28, 2026
  2. cptnm3 commented on Jul 29, 2026

    @cptnm3
    Contributor

    @planetf1 @ajbozarth I would like to work on this. Could you please assign this to me?

  3. ajbozarth commented on Jul 29, 2026

    @ajbozarth
    Member

    Given this is recently opened by @planetf1 I'll defer to him on you picking this up just in case he intended to do it himself (fyi he's on London time and might not see this till tomorrow)

  4. cptnm3 commented on Jul 29, 2026

    @cptnm3
    Contributor

    Makes sense. Thank you @ajbozarth

  5. psschwei commented on Jul 29, 2026

    @psschwei
    Contributor

    doesn't this already exist?

    model_options: dict | None = None,

  6. planetf1 commented on Jul 30, 2026

    @planetf1
    ContributorAuthor

    Yes can hold on this one for now. I had some findings in some analytics I was doing of melea/granite behaviour. The issue was indended to be opened yet.

    I’ll assign it to myself to handle for now as it needs more clarification and validation

  7. self-assigned this
    on Jul 30, 2026
  8. planetf1 commented on Aug 10, 2026

    @planetf1
    ContributorAuthor

    Closing as already implemented.

    @psschwei was right — instruct() already accepts model_options: dict | None = None as a parameter (see mellea/stdlib/session.py line 575). Per-call model options (including extra_body contents) can be passed today:

    ```python
    result = await session.instruct(
    prompt,
    model_options={"extra_body": {"chat_template_kwargs": {"enable_thinking": True, "thinking_budget": 5000}}},
    )
    ```

    The underlying need (forwarding thinking-tier flags without tearing down the session) is addressed by #1453 (default_extra_body on OpenAIBackend) for the construction-time case. The per-call override already exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions