How to change the value of request body? #147

Closed
opened 2026-02-16 00:18:59 -05:00 by yindo · 1 comment
Owner

Originally created by @Songjiadong on GitHub (Mar 23, 2024).

I want to change the request body value of simple_invoke,would you give me a demo? I can't change request body value.
Based on my experience with Java and C #, I believe this can be solved, but I am not very familiar with FastAPI . Can you help me?
Thanks!

def add_one(x: int) -> int:
    """Add one to the given number."""
    return x + 1


chain = RunnableLambda(add_one)

api_handler = APIHandler(chain, path="/simple")


# First register the endpoints without documentation
@app.post("/simple/invoke", include_in_schema=False)
async def simple_invoke(request: Request) -> Response:
    """Handle a request."""
    ### NOTE: I want change the value of request body at this position !!!! for example:  if  value of request body equals '1',I want change it to '100'
    return await api_handler.invoke(request)
Originally created by @Songjiadong on GitHub (Mar 23, 2024). I want to change the request body value of `simple_invoke`,would you give me a **demo**? I can't change request body value. Based on my experience with Java and C #, I believe this can be solved, but I am not very familiar with FastAPI . Can you help me? Thanks! ```py def add_one(x: int) -> int: """Add one to the given number.""" return x + 1 chain = RunnableLambda(add_one) api_handler = APIHandler(chain, path="/simple") # First register the endpoints without documentation @app.post("/simple/invoke", include_in_schema=False) async def simple_invoke(request: Request) -> Response: """Handle a request.""" ### NOTE: I want change the value of request body at this position !!!! for example: if value of request body equals '1',I want change it to '100' return await api_handler.invoke(request) ```
yindo closed this issue 2026-02-16 00:18:59 -05:00
Author
Owner

@eyurtsev commented on GitHub (Mar 27, 2024):

What do you need to accomplish? I'm sure there's a way to mutate the Request body, but it's not super common practice.

If you're already mutating the request body, why not avoid using langserve and just use the underlying runnable with fast API directly?

Chat gpt suggests doing something like this:

        body = await request.json()
        
        # Modify the body
        body["extra_field"] = "extra_value"
        
        # Create a new request with the modified body
        request.scope["body"] = json.dumps(body).encode("utf-8")     
@eyurtsev commented on GitHub (Mar 27, 2024): What do you need to accomplish? I'm sure there's a way to mutate the Request body, but it's not super common practice. If you're already mutating the request body, why not avoid using langserve and just use the underlying runnable with fast API directly? Chat gpt suggests doing something like this: ```python body = await request.json() # Modify the body body["extra_field"] = "extra_value" # Create a new request with the modified body request.scope["body"] = json.dumps(body).encode("utf-8") ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#147