pass parameter betweens each Runnables in a chain #126

Closed
opened 2026-02-16 00:18:54 -05:00 by yindo · 0 comments
Owner

Originally created by @shaojun on GitHub (Feb 22, 2024).

Hi,
I have a CustomUserType input request, and there's a field of shared_pass_through_parameter that contains non-nature language data which would be used in any followed Runnables (except the LLM node).
like the below sample code, the purpose of this field is to pass to final RunnableLambda(output_process) to output a customized content.

app = FastAPI(
    title="LangChain Server",
    version="1.0",
    description="A simple api server using Langchain's Runnable interfaces",
)

# model = ChatModelBuilder.Create()


class MyCustomRequest(CustomUserType):
    content: str
    prompt: str
    shared_pass_through_parameter: str = "No-natural language content"


def process(request: MyCustomRequest):
    from pydantic import BaseModel, create_model
    import json
    prompt = ChatPromptTemplate.from_messages([
        ("system", '{system_prompt}'),
        ("human", '{user_content}')
    ])
    return prompt.invoke({"system_prompt": request.prompt, "user_content": request.content})


def output_process(input: Any):
    # !!!HOW TO GET the shared_pass_through_parameter
    # if input.shared_pass_through_parameter == 'test':
    #     print('shared_pass_through_parameter is test')
    return input


model = ChatOpenAI()
add_routes(
    app,
    RunnableLambda(process).with_types(
        input_type=MyCustomRequest) | model | RunnableLambda(output_process),
    path="/MyService1",
)
if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=9000)

Obvious that field will be dropped as it's not included into the output of process.
I can see a solution that wrap all the route chain into a single function, but that will lose the feature of streaming of LLM.
any suggestion?

Originally created by @shaojun on GitHub (Feb 22, 2024). Hi, I have a `CustomUserType` input request, and there's a field of `shared_pass_through_parameter` that contains `non-nature language` data which would be used in any followed `Runnables` (except the `LLM` node). like the below sample code, the purpose of this field is to pass to final `RunnableLambda(output_process)` to output a customized content. ``` app = FastAPI( title="LangChain Server", version="1.0", description="A simple api server using Langchain's Runnable interfaces", ) # model = ChatModelBuilder.Create() class MyCustomRequest(CustomUserType): content: str prompt: str shared_pass_through_parameter: str = "No-natural language content" def process(request: MyCustomRequest): from pydantic import BaseModel, create_model import json prompt = ChatPromptTemplate.from_messages([ ("system", '{system_prompt}'), ("human", '{user_content}') ]) return prompt.invoke({"system_prompt": request.prompt, "user_content": request.content}) def output_process(input: Any): # !!!HOW TO GET the shared_pass_through_parameter # if input.shared_pass_through_parameter == 'test': # print('shared_pass_through_parameter is test') return input model = ChatOpenAI() add_routes( app, RunnableLambda(process).with_types( input_type=MyCustomRequest) | model | RunnableLambda(output_process), path="/MyService1", ) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=9000) ``` Obvious that field will be dropped as it's not included into the output of `process`. I can see a solution that wrap all the route chain into a single function, but that will lose the feature of `streaming of LLM`. any suggestion?
yindo closed this issue 2026-02-16 00:18:54 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langserve#126