Skip to content

Coding a chain as a pyfunc model with pre- and post-processing

MLflow's pyfunc flavor lets you wrap an LLM chain (retrieval, prompt construction, model calls, and response parsing) inside a single custom Python class so it can be logged, versioned, and deployed as one deployable unit. By subclassing mlflow.pyfunc.PythonModel and implementing predict() (and optionally load_context()), you control exactly how inputs are pre-processed and outputs are post-processed before returning them to the caller.

1 · Learn the must-know

  • Custom logic goes in the predict() method, which receives context and model_input and must return the final formatted output, enabling arbitrary pre-processing (e.g., prompt templating, query rewriting) and post-processing (e.g., parsing, filtering, guardrails) in plain Python code.
  • Use load_context() to load heavy artifacts (retrievers, vector store clients, tokenizers, model handles) once at model load time rather than on every predict() call, improving latency.
  • Log the model with mlflow.pyfunc.log_model(), specifying the python_model object, any code_path/artifacts dependencies, and a signature/input example so the model registry and serving endpoints know the expected schema.
  • A signature (input/output schema) is required for good practice and is often necessary for compatibility with Model Serving and validation of downstream consumers.
  • Pyfunc models registered in Unity Catalog or the MLflow Model Registry can be deployed directly to a Databricks Model Serving endpoint, exposing the entire pre-process/inference/post-process chain as a single REST API.
  • Because pyfunc wraps everything in one artifact, all dependencies (prompt templates, retrieval clients, third-party SDKs) must be captured via conda/pip requirements or code_path so the packaged model is self-contained and reproducible at serving time.

2 · Check your understanding

Check this objectiveFree · always available

A Generative AI Engineer is building a RAG-based chatbot and must log the chain as an MLflow model so that a custom step extracts only the latest user turn from a multi-turn chat payload before retrieval, and a second step strips internal citation tags such as [doc_1] from the final answer before it reaches the user. Which approach should the engineer use to package this logic?

Your objective map0 tried · 0 answered correctly · 56 untouched

What you have tried across Databricks GenAI Engineer's objectives, not a readiness score.

Design Applications10.71% of the exam*0 of 6 tried
Data Preparation14.29% of the exam*0 of 8 tried
Application Development23.21% of the exam*0 of 13 tried
Assembling and Deploying Applications26.79% of the exam*0 of 15 tried
Governance7.14% of the exam*0 of 4 tried
Evaluation and Monitoring17.86% of the exam*0 of 10 tried

* Our estimate. Databricks publishes no section weights.

3 · Keep going