Skip to content

Clean up internal LLM API#484

Merged
kiranandcode merged 11 commits intomasterfrom
eb-llm-message-ops
Jan 31, 2026
Merged

Clean up internal LLM API#484
kiranandcode merged 11 commits intomasterfrom
eb-llm-message-ops

Conversation

@eb8680
Copy link
Contributor

@eb8680 eb8680 commented Jan 13, 2026

This refactoring PR reorganizes the internals of handlers.llm around four new Operations, each of which (call_assistant, call_user, call_system, call_tool) is responsible for constructing all Messages for one role ("assistant", "user", "system", "tool") in the completions API.

It also avoids the anti-pattern of handling Template.__apply__, except as a temporary expedient in RetryLLMHandler, and includes some general code cleanup of completions.py.

I'm putting it up as a draft for discussion, since I haven't finished getting tests to pass and there are still some outstanding design questions, particularly around the desired semantics of failure and repetition.

@eb8680 eb8680 changed the base branch from staging-llm to master January 15, 2026 17:08
@eb8680 eb8680 changed the base branch from master to staging-llm January 15, 2026 17:23
@eb8680 eb8680 force-pushed the eb-llm-message-ops branch from 061a342 to fa62848 Compare January 15, 2026 17:30
@eb8680 eb8680 changed the base branch from staging-llm to master January 15, 2026 17:30
@kiranandcode kiranandcode marked this pull request as ready for review January 28, 2026 20:13
@kiranandcode kiranandcode force-pushed the eb-llm-message-ops branch 2 times, most recently from 8e8a624 to 155f4e6 Compare January 28, 2026 20:39
@kiranandcode kiranandcode requested a review from jfeser January 28, 2026 20:40
Copy link
Contributor Author

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

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

I know I suggested this API, but to address #495 I'm realizing we probably want to modify the interfaces of these new operations so that they return both a Message and correctly decoded data:

type ToolCallId = ...


class DecodedToolCall[T](typing.NamedTuple):
  tool: Tool[..., T]
  bound_args: inspect.BoundArguments
  id: ToolCallId
  final: bool


@Operation.define
def call_assistant[T](
  messages: Sequence[Message],
  encoding: Encodable[T, pydantic.BaseModel | str],
  **kwargs
) -> tuple[Message, T | None, Sequence[DecodedToolCall]]:
  ...
  message: Message = ...
  if message.tool_calls:
    decoded_tool_calls: Sequence[DecodedToolCall] = ...
    return message, None, decoded_tool_calls
  else:
    result: T = encoding.decode(encoding.deserialize(message.content))
    return message, result, ()


@Operation.define
def call_tool[T](
  tc: DecodedToolCall[T],
) -> tuple[Message, T]:
  result: T = tc.tool(*tc.bound_args.args, **tc.bound_args.kwargs)
  encoding = Encodable.define(type(result))
  msg: Message = Message(role="tool", content=encoding.serialize(encoding.encode(result)), tool_call_id=tc.id)
  return msg, result

@kiranandcode
Copy link
Contributor

@eb8680 , ready for review!

Copy link
Contributor Author

@eb8680 eb8680 left a comment

Choose a reason for hiding this comment

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

This removes RetryLLMHandler, is that still intended?

@kiranandcode
Copy link
Contributor

Leaving RetryLLMHandler for another PR

@eb8680
Copy link
Contributor Author

eb8680 commented Jan 30, 2026

OK, in that case it's good to merge. You'll have to approve and merge it yourself since I'm the original author of the PR.

@kiranandcode
Copy link
Contributor

Gotcha, will do once tests pass! Rebasing the synthesis on top of this, and working on that in parallel

@kiranandcode kiranandcode merged commit a4a5086 into master Jan 31, 2026
6 checks passed
@kiranandcode kiranandcode deleted the eb-llm-message-ops branch January 31, 2026 00:22
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.

2 participants