Skip to content

feat: accept metadata transformer for LangChain callback handler#516

Merged
whoseoyster merged 3 commits into
mainfrom
cid/langchain-handler-metadata
Aug 29, 2025
Merged

feat: accept metadata transformer for LangChain callback handler#516
whoseoyster merged 3 commits into
mainfrom
cid/langchain-handler-metadata

Conversation

@gustavocidornelas
Copy link
Copy Markdown
Contributor

@gustavocidornelas gustavocidornelas commented Aug 27, 2025

Pull Request

Summary

Introduces an optional kwarg metadata_transformer to the LangChain callback handler.

metadata_transformer should be a transformation function (Callable[[Dict[str, Any]], Dict[str, Any]]) that is applied to the metadata object currently logged by the callback handler. This gives the user fine-grained control about the metadata logged.

Correctly parses usage information when streaming responses

The previous version of the callback handler didn't have the on_new_token method implemented. The current implementation handles the usage information, which is streamed at the very end when stream_usage=True.

Context logging

Now, we're correctly logging the context on retrieval steps as well as on chains that contain source_documents. This enables context-dependent metrics on the platform

Usage

For example, if the user wants to exclude metadata fields that start with langgraph, a possible metadata transformer would be:

def metadata_filter(metadata: Dict[str, Any]) -> Dict[str, Any]:
    return {k: v for k, v in metadata.items() if not k.startswith("langgraph")}


from openlayer.lib.integrations import langchain_callback

openlayer_handler = langchain_callback.OpenlayerHandler(metadata_transformer=metadata_filter)

For streaming, the usage is:

chat = AzureChatOpenAI(...)
for chunk in chat.stream("What's the meaning of life?", stream_usage=True):
    print(chunk.text(), end="")

@whoseoyster whoseoyster merged commit a1846e0 into main Aug 29, 2025
5 checks passed
@whoseoyster whoseoyster deleted the cid/langchain-handler-metadata branch August 29, 2025 15:51
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