how to write a dynamic json output parser chain #113

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

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

I'm creating a service, besides the content and prompt, that allows input a json sample str which for constrait the output, and output the final expecting json, the sample code:

from langchain.output_parsers import PydanticOutputParser
from langchain_core.output_parsers import JsonOutputParser
class OneContentToOneRequest(CustomUserType):
    content: str
    prompt: str
    output_format_sample_json_str: str = None

model = ChatOpenAI()
def create_prompt(request: OneContentToOneRequest):
    from pydantic import BaseModel, create_model
    import json
    output_format_sample_json = json.loads(
        request.output_format_sample_json_str)
    output_format_pydantic_model = create_model(
        'output_format_pydantic_model', **output_format_sample_json)
    output_format_pydantic_model = output_format_pydantic_model
    # Set up a parser + inject instructions into the prompt template.
    parser = PydanticOutputParser(
        pydantic_object=output_format_pydantic_model)
    return {"system_prompt": request.prompt, "format_instructions": parser.get_format_instructions(), "user_content": request.content}

prompt = ChatPromptTemplate.from_messages([
            ("system", '{system_prompt}\n{format_instructions}'),
            ("human", '{user_content}')
        ])
add_routes(
            app,
            RunnableLambda(create_prompt).with_types(
                input_type=OneContentToOneRequest) | prompt | model | how_to_write_this_dynamic_parser ,
            path="/MyService1",
        )
if __name__ == "__main__":
    import uvicorn

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

this code obvious can't work as the last part of chain: how_to_write_this_dynamic_parser has not existed and defined yet.
could you help?

Originally created by @shaojun on GitHub (Feb 2, 2024). I'm creating a service, besides the content and prompt, that allows input a `json sample str` which for constrait the output, and output the final expecting json, the sample code: ``` from langchain.output_parsers import PydanticOutputParser from langchain_core.output_parsers import JsonOutputParser class OneContentToOneRequest(CustomUserType): content: str prompt: str output_format_sample_json_str: str = None model = ChatOpenAI() def create_prompt(request: OneContentToOneRequest): from pydantic import BaseModel, create_model import json output_format_sample_json = json.loads( request.output_format_sample_json_str) output_format_pydantic_model = create_model( 'output_format_pydantic_model', **output_format_sample_json) output_format_pydantic_model = output_format_pydantic_model # Set up a parser + inject instructions into the prompt template. parser = PydanticOutputParser( pydantic_object=output_format_pydantic_model) return {"system_prompt": request.prompt, "format_instructions": parser.get_format_instructions(), "user_content": request.content} prompt = ChatPromptTemplate.from_messages([ ("system", '{system_prompt}\n{format_instructions}'), ("human", '{user_content}') ]) add_routes( app, RunnableLambda(create_prompt).with_types( input_type=OneContentToOneRequest) | prompt | model | how_to_write_this_dynamic_parser , path="/MyService1", ) if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=9000) ``` this code obvious can't work as the last part of chain: `how_to_write_this_dynamic_parser ` has not existed and defined yet. could you help?
yindo closed this issue 2026-02-16 00:18:52 -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#113