From 2ec6dac8bd1c1b3f76404c2aa24b07203e25d9b4 Mon Sep 17 00:00:00 2001 From: Twisha Bansal Date: Fri, 6 Feb 2026 17:10:15 +0530 Subject: [PATCH] address comments --- docs/en/samples/pre_post_processing/python.md | 10 ++++++++++ .../pre_post_processing/python/langchain/agent.py | 13 ++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/en/samples/pre_post_processing/python.md b/docs/en/samples/pre_post_processing/python.md index ebcd82a8fb..4a8b90fe93 100644 --- a/docs/en/samples/pre_post_processing/python.md +++ b/docs/en/samples/pre_post_processing/python.md @@ -29,3 +29,13 @@ For more information, see the [LangChain Middleware documentation](https://docs. You can also add model-level (`wrap_model`) and agent-level (`before_agent`, `after_agent`) hooks to intercept messages at different stages of the execution loop. See the [LangChain Middleware documentation](https://docs.langchain.com/oss/python/langchain/middleware/custom#wrap-style-hooks) for details on these additional hook types. {{% /tab %}} {{< /tabpane >}} + +## Results + +The output should look similar to the following. Note that exact responses may vary due to the non-deterministic nature of LLMs and differences between orchestration frameworks. + +``` +AI: Booking Confirmed! You earned 500 Loyalty Points with this stay. + +AI: Error: Maximum stay duration is 14 days. +``` \ No newline at end of file diff --git a/docs/en/samples/pre_post_processing/python/langchain/agent.py b/docs/en/samples/pre_post_processing/python/langchain/agent.py index a363c2bad3..27b3f9da4d 100644 --- a/docs/en/samples/pre_post_processing/python/langchain/agent.py +++ b/docs/en/samples/pre_post_processing/python/langchain/agent.py @@ -47,7 +47,13 @@ async def enforce_business_rules(request, handler): except ValueError: pass # Ignore invalid date formats - return await handler(request) + # PRE: Code here runs BEFORE the tool execution + + # EXEC: Execute the tool (or next middleware) + result = await handler(request) + + # POST: Code here runs AFTER the tool execution + return result # Post processing @@ -58,8 +64,12 @@ async def enrich_response(request, handler): Adds loyalty points information to successful bookings. Standardizes output format. """ + # PRE: Code here runs BEFORE the tool execution + + # EXEC: Execute the tool (or next middleware) result = await handler(request) + # POST: Code here runs AFTER the tool execution if isinstance(result, ToolMessage): content = str(result.content) tool_name = request.tool_call["name"] @@ -79,6 +89,7 @@ async def main(): system_prompt=system_prompt, model=model, tools=tools, + # add any pre and post processing methods middleware=[enforce_business_rules, enrich_response], )