hermes - 💡(How to fix) Fix [Feature]: FR: Implement chat history normalization / adapter for strict backends (LiteRT, custom OpenAI shims)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

When running hermes-agent against local or strict inference backends (such as Google's LiteRT mobile server or certain custom OpenAI-compatible shims), OpenAI-style API calls often fail with strict validation errors.

The two most common blockers are:

  1. Last message must be from user: Triggered when the agent processes a tool execution loop. The sequence ends with a tool or system message containing the command output, which strict backends reject as a final message.
  2. Consecutive roles or invalid role types: Backends designed around a strict conversation turn philosophy enforce alternating user -> assistant -> user flows and may completely reject system or tool roles in the middle of the payload.

Root Cause

Current Behavior

During agent tool execution loops, stream_kwargs["messages"] contains raw metadata and tool execution logs. When passed directly to client.chat.completions.create(), strict servers reject the payload with HTTP 400 because the strict chat template formatting constraints are violated.

Fix Action

Fix / Workaround

Introduce a message history normalization adapter (sanitizer) in agent/chat_completion_helpers.py right before dispatching the request to the client.

Example Patch Location

In agent/chat_completion_helpers.py around the streaming creation point:

Code Example

# Before: stream = request_client_holder["client"].chat.completions.create(**stream_kwargs)

if "messages" in stream_kwargs and stream_kwargs["messages"]:
    # Apply normalization/adaptation logic here based on a config flag 
    # (e.g., agent.config.strict_chat_template = True)
    stream_kwargs["messages"] = normalize_chat_history(stream_kwargs["messages"])

stream = request_client_holder["client"].chat.completions.create(**stream_kwargs)


### Alternatives Considered

_No response_

### Feature Type

Gateway / messaging improvement

### Scope

Small (single file, < 50 lines)

### Contribution

- [ ] I'd like to implement this myself and submit a PR

### Debug Report (optional)
RAW_BUFFERClick to expand / collapse

Problem or Use Case

Description

When running hermes-agent against local or strict inference backends (such as Google's LiteRT mobile server or certain custom OpenAI-compatible shims), OpenAI-style API calls often fail with strict validation errors.

The two most common blockers are:

  1. Last message must be from user: Triggered when the agent processes a tool execution loop. The sequence ends with a tool or system message containing the command output, which strict backends reject as a final message.
  2. Consecutive roles or invalid role types: Backends designed around a strict conversation turn philosophy enforce alternating user -> assistant -> user flows and may completely reject system or tool roles in the middle of the payload.

Current Behavior

During agent tool execution loops, stream_kwargs["messages"] contains raw metadata and tool execution logs. When passed directly to client.chat.completions.create(), strict servers reject the payload with HTTP 400 because the strict chat template formatting constraints are violated.

Proposed Solution

Introduce a message history normalization adapter (sanitizer) in agent/chat_completion_helpers.py right before dispatching the request to the client.

The adapter should safely prepare the payload for strict environments by:

  • Collapsing consecutive roles: Merging sequential messages from the same role into a single message block to preserve strict alternation.
  • Mapping invalid roles: Masking intermediate tool or system outputs as user messages if the target provider endpoint requires it.
  • Ensuring User-Role Finality: Append a minor trigger/prompt (e.g., "Proceed with the final response based on the tool output above.") under the user role if the history naturally ends with a tool output block.
  • Stringifying Empty/Object Content: Ensuring that if content is null but contains tool_calls payloads, it gracefully stringifies into a text block for engines that do not natively parse raw JSON nodes inside the message array.

Example Patch Location

In agent/chat_completion_helpers.py around the streaming creation point:

# Before: stream = request_client_holder["client"].chat.completions.create(**stream_kwargs)

if "messages" in stream_kwargs and stream_kwargs["messages"]:
    # Apply normalization/adaptation logic here based on a config flag 
    # (e.g., agent.config.strict_chat_template = True)
    stream_kwargs["messages"] = normalize_chat_history(stream_kwargs["messages"])

stream = request_client_holder["client"].chat.completions.create(**stream_kwargs)


### Alternatives Considered

_No response_

### Feature Type

Gateway / messaging improvement

### Scope

Small (single file, < 50 lines)

### Contribution

- [ ] I'd like to implement this myself and submit a PR

### Debug Report (optional)

```shell
OS: Android (Termux environment)
Local Server: LiteRT / local model deployment shims
Models tested: Gemma-4-it

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING