Skip to main content
This is an interactive notebook. You can run it locally or use the following links:
Generating high-quality code with proper structure, documentation, and tests is a challenging task. This guide is for developers who want to build an LLM-powered code generation workflow and systematically measure its quality. This notebook demonstrates how to create a code-generation pipeline that produces Python functions evaluated against the HumanEval test suite. The pipeline uses Weave for evaluation comparison and tracking and OpenAI’s GPT models for code generation with structured outputs.
Weave evaluation dashboard comparing code generation runs

Why use Weave

This tutorial uses Weave to implement and evaluate a code generation pipeline. You learn how to:
  • Track your LLM pipeline: Log inputs, outputs, and intermediate steps of your code generation process.
  • Evaluate LLM outputs: Create and compare evaluations of your generated code with debugging tools and visualizations.

Set up the environment

Set up your environment and import the necessary libraries. These dependencies provide the formatting tools, dataset loader, and OpenAI and Weave clients used throughout the pipeline.
Weave automatically tracks OpenAI API calls, including inputs, outputs, and metadata. You don’t need to add any additional logging code for your OpenAI interactions. Weave handles it in the background.

Structured outputs and Pydantic models

This code generation pipeline uses OpenAI’s structured outputs mode and Pydantic models to produce consistent and well-formatted responses from the language model. This approach offers several advantages:
  • Type safety: Defining Pydantic models for the expected outputs enforces a strict structure for the generated code, program runners, and unit tests.
  • Easier parsing: The structured output mode parses the model’s response directly into the predefined Pydantic models, reducing the need for complex post-processing.
  • Improved reliability: Specifying the exact format you expect reduces the likelihood of unexpected or malformed outputs from the language model.
The following example defines Pydantic models and uses them with OpenAI’s structured outputs:

Implement a code formatter

To produce consistent and clean code output, implement a CodeFormatter class using Weave operations. This formatter applies linting and styling rules to the generated code, program runner, and unit tests.
This CodeFormatter class provides several Weave operations to clean and format the generated code:
  • Replacing escaped newlines with actual newlines.
  • Removing unused imports and variables.
  • Sorting imports.
  • Applying PEP 8 formatting.
  • Adding missing imports.

Define the CodeGenerationPipeline

Weave trace of a code generation pipeline run
With the formatter in place, the next step is to implement the core code generation logic that ties the prompt, the LLM call, and the formatter together. This example uses a weave.Model so that the model is automatically versioned when it changes. The model_name is kept as an attribute so that you can experiment with it and diff and compare it in Weave. Function calls are tracked with @weave.op so the inputs and outputs are logged to help with error tracking and debugging.
This CodeGenerationPipeline class encapsulates the code generation logic as a Weave Model, providing several benefits:
  • Automatic experiment tracking: Weave captures inputs, outputs, and parameters for each run of the model.
  • Versioning: Changes to the model’s attributes or code are automatically versioned, creating a history of how your code generation pipeline evolves over time.
  • Reproducibility: The versioning and tracking let you reproduce any previous result or configuration of your code generation pipeline.
  • Hyperparameter management: Model attributes (like model_name) are defined and tracked across different runs, facilitating experimentation.
  • Integration with Weave ecosystem: Using weave.Model connects your pipeline to other Weave tools, such as evaluations and serving capabilities.

Implement evaluation metrics

To assess the quality of the generated code, implement evaluation metrics using a weave.Scorer subclass. This runs score on every model_output from the dataset. model_output comes from the output of the predict function in the weave.Model. prompt is taken from the human-eval dataset.
These evaluation functions run the generated code and return a boolean value indicating whether the code passed the test provided from the dataset.
Weave trace of a HumanEval scorer evaluating generated code

Create a Weave Dataset and run evaluation

With the pipeline and scorer defined, the final step is to assemble the evaluation dataset and run it end-to-end. To evaluate the pipeline, create a Weave Dataset and run an evaluation:
This code creates a dataset with the sample prompts, defines the HumanEval test scorer, and runs an evaluation of the code generation pipeline. After the evaluation completes, results are available in the Weave UI for inspection and comparison across runs.
Weave evaluation dashboard showing HumanEval scorer results

Conclusion

This example demonstrates how to implement a code generation pipeline using Weave and OpenAI’s language models. You learned how to:
  • Create Weave operations for each step of the code generation process.
  • Wrap the pipeline in a Weave Model for streamlined tracking and evaluation.
  • Implement custom evaluation metrics using Weave operations.
  • Create a dataset and run an evaluation of the pipeline.
Weave tracks inputs, outputs, and intermediate steps throughout the code generation process, making it easier to debug, optimize, and evaluate your LLM application. For more information about Weave and its capabilities, see the Weave documentation. You can extend this example to handle larger datasets, implement more sophisticated evaluation metrics, or integrate with other LLM workflows.