Skip to content

feat: add as_generic_chat_history function to convert any Context to …#1007

Open
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:issue932
Open

feat: add as_generic_chat_history function to convert any Context to …#1007
akihikokuroda wants to merge 3 commits intogenerative-computing:mainfrom
akihikokuroda:issue932

Conversation

@akihikokuroda
Copy link
Copy Markdown
Member

@akihikokuroda akihikokuroda commented May 4, 2026

Misc PR

Type of PR

  • Bug Fix
  • New Feature
  • Documentation
  • Other

Description

Add as_generic_chat_history() function to handle heterogeneous contexts with
configurable formatter. Unlike as_chat_history(), this gracefully handles
arbitrary component types by converting them to strings, with optional logging
for unknown types.

  • as_generic_chat_history(ctx, formatter=None) converts any Context to Messages
  • Default formatter logs warnings for unknown component types
  • Support custom formatter parameter for domain-specific type handling
  • Handle ModelOutputThunk, CBlock, and arbitrary components seamlessly
  • CBlock → Message(role="user"), MOT → Message(role="assistant")
  • Add 7 comprehensive tests covering all conversion scenarios

Testing

  • Tests added to the respective file if code was changed
  • New code has 100% coverage if code as added
  • Ensure existing tests and github automation passes (a maintainer will kick off the github automation when the rest of the PR is populated)

Attribution

  • AI coding assistants used: claude

…Message

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@akihikokuroda akihikokuroda requested a review from a team as a code owner May 4, 2026 18:59
@akihikokuroda akihikokuroda requested review from markstur and planetf1 May 4, 2026 18:59
@github-actions github-actions Bot added the enhancement New feature or request label May 4, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 4, 2026

The PR description has been updated. Please fill out the template for your PR to be reviewed.

return c.parsed_repr
# Use value if parsed_repr is None or not a Message
content = (
str(c.value) if c.parsed_repr is None else formatter(c.parsed_repr)
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 think we also need to consider the case if c.parsed_repr is a string (I believe that's what a MOT will return after a completed CBlock action (?) )

quick way to test:

mot = ModelOutputThunk(value='reply text')
mot.parsed_repr = 'reply text'
as_generic_chat_history(ChatContext().add(Message('user','hi')).add(mot))
# → WARNING: Unknown component type str ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, it was an issue. I fix it. Thanks!

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.

should we add a test case for it too?

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@psschwei
Copy link
Copy Markdown
Member

psschwei commented May 5, 2026

there's a typo in the PR title: s/ ant / any /

@akihikokuroda akihikokuroda changed the title feat: add as_generic_chat_history function to convert ant Context to … feat: add as_generic_chat_history function to convert any Context to … May 5, 2026
Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix: as_chat_history is outdated and functionality doesn't match it's name

2 participants