---
title: "Example: Langfuse Prompt Management for OpenAI functions (Python)"
description: Cookbook on how to use Langfuse Prompt Management to version control prompts collaboratively when using OpenAI functions.
category: Prompt Management
---

# Example: Langfuse Prompt Management for OpenAI functions (Python)

Langfuse [Prompt Management](https://langfuse.com/docs/prompts) helps to version control and manage prompts collaboratively in one place. This example demostrates how to use the flexible `config` object on Langfuse prompts to store function calling options and model parameters.

## Setup

```python
%pip install langfuse openai --upgrade
```

```python
import os

# Get keys for your project
os.environ.setdefault("LANGFUSE_PUBLIC_KEY", "");
os.environ.setdefault("LANGFUSE_SECRET_KEY", "");
os.environ.setdefault("LANGFUSE_BASE_URL", "https://cloud.langfuse.com");

# OpenAI key
os.environ.setdefault("OPENAI_API_KEY", "");
```

```python
from langfuse import get_client

langfuse = get_client()
```

    True

## Add prompt to Langfuse Prompt Management

We add the prompt used in this example via the SDK. Alternatively, you can also edit and version the prompt in the Langfuse UI.

- `Name` that identifies the prompt in Langfuse Prompt Management
- Prompt with `json_schema` variable
- Config including `model_name`, `temperature`, and `json_schema`
- `labels` to include `production` to immediately use the prompt as the default

```python
langfuse.create_prompt(
    name="story_summarization",
    prompt="Extract the key information from this text and return it in JSON format. Use the following schema: {{json_schema}}",
    config={
        "model":"gpt-3.5-turbo-1106",
        "temperature": 0,
        "json_schema":{
            "main_character": "string (name of protagonist)",
            "key_content": "string (1 sentence)",
            "keywords": "array of strings",
            "genre": "string (genre of story)",
            "critic_review_comment": "string (write similar to a new york times critic)",
            "critic_score": "number (between 0 bad and 10 exceptional)"
        }
    },
    labels=["production"]
);
```

Prompt in Langfuse UI

![Langfuse Prompt Management](https://langfuse.com/images/docs/prompt-management-with-config-for-openai-functions.png)

## Example application

### Get current prompt version from Langfuse

```python
prompt = langfuse.get_prompt("story_summarization")
```

We can now use the prompt to compile our system message

```python
prompt.compile(json_schema="TEST SCHEMA")
```

    'Extract the key information from this text and return it in JSON format. Use the following schema: TEST SCHEMA'

And it includes the config object

```python
prompt.config
```

```
{'model': 'gpt-3.5-turbo-1106',
 'json_schema': {'genre': 'string (genre of story)',
  'keywords': 'array of strings',
  'key_content': 'string (1 sentence)',
  'critic_score': 'number (between 0 bad and 10 exceptional)',
  'main_character': 'string (name of protagonist)',
  'critic_review_comment': 'string (write similar to a new york times critic)'},
 'temperature': 0}
 ```

### Create example function

In this example we use the native [Langfuse OpenAI integration](https://langfuse.com/integrations/model-providers/openai-py) by importing from `langfuse.openai`. This enables [tracing](https://langfuse.com/docs/tracing) in Langfuse and is not required for using Langfuse prompts management.

```python
from langfuse.openai import OpenAI
client = OpenAI()
```

Use Langfuse prompt to construct the `summarize_story` example function.

**Note:** You can link the generation in Langfuse Tracing to the prompt version by passing the `langfuse_prompt` parameter to the `create` method. Have a look at our [prompt management docs](https://langfuse.com/docs/prompts/get-started#link-with-langfuse-tracing-optional) to learn how to link prompt and generation with other integrations and SDKs.

```python
import json

def summarize_story(story):
  # Stringify the JSON schema
  json_schema_str = ', '.join([f"'{key}': {value}" for key, value in prompt.config["json_schema"].items()])

  # Compile prompt with stringified version of json schema
  system_message = prompt.compile(json_schema=json_schema_str)

  # Format as OpenAI messages
  messages = [
      {"role":"system","content": system_message},
      {"role":"user","content":story}
  ]

  # Get additional config
  model = prompt.config["model"]
  temperature = prompt.config["temperature"]

  # Execute LLM call
  res = client.chat.completions.create(
    model = model,
    temperature = temperature,
    messages = messages,
    response_format = { "type": "json_object" },
    langfuse_prompt = prompt # capture used prompt version in trace
  )

  # Parse response as JSON
  res = json.loads(res.choices[0].message.content)

  return res
```

### Execute it

```python
# Thanks ChatGPT for the story
STORY = """
In a bustling city where the nighttime glittered with neon signs and the rush never calmed, lived a lonely cat named Whisper. Amidst the ceaseless clatter, Whisper discovered an abandoned hat one day. To her enigmatic surprise, this was no ordinary accessory; it had the unusual power to make her invisible to any onlooker.
Whisper, now carrying a peculiar power, started a journey that was unexpected. She became a benevolent spirit to the less fortunate, the homeless people who equally shared the cold nights with her. Nights that were once barren turned miraculous as warm meals mysteriously appeared to those who needed them most. No one could see her, yet her actions spoke volumes, turning her into an unsung hero in the hidden corners of the city.
As she carried on with her mysterious deed, she found an unanticipated reward. Joy started to kindle in her heart, born not from the invisibility, but from the result of her actions; the growing smiles on the faces of those she surreptitiously helped. Whisper might have remained unnoticed to the world, but amidst her secret kindness, she discovered her true happiness.
"""
```

```python
summary = summarize_story(STORY)
```

```
{'genre': 'Fantasy',
 'keywords': ['lonely cat',
  'invisible',
  'benevolent spirit',
  'unsung hero',
  'mysterious deed',
  'true happiness'],
 'key_content': 'In a bustling city, a lonely cat named Whisper discovers an abandoned hat with the power to make her invisible, leading her to become a benevolent spirit and unsung hero to the less fortunate.',
 'critic_score': 9,
 'main_character': 'Whisper',
 'critic_review_comment': "Whisper's journey from loneliness to self-discovery through acts of kindness is a heartwarming and enchanting tale that captivates the reader with its magical elements and profound message about true happiness."}
 ```

## View trace in Langfuse

As we used the native Langfuse integration with the OpenAI SDK, we can view the trace in Langfuse.

![Trace of OpenAI functions in Langfuse](https://langfuse.com/images/docs/openai-functions-trace-with-prompt-management.png)

## Iterate on prompt in Langfuse

We can now iterate on the prompt in Langfuse UI including model parameters and function calling options without changing the code or redeploying the application.

<!-- agent-instructions -->

---

## Agent Instructions

This page is part of the [Langfuse](https://langfuse.com) documentation, published as plain Markdown for AI agents. Every page is available as Markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` header. This page: `https://langfuse.com/guides/cookbook/prompt_management_openai_functions.md`.

### Querying these docs

If the answer is not on this page, query the documentation instead of guessing:

- **Semantic search** across all Langfuse docs, returning an answer with the relevant pages and excerpts. Ask a specific, self-contained question:

  ```bash
  curl -sG "https://langfuse.com/api/search-docs" --data-urlencode "query=How do I trace a LangGraph agent?"
  ```

- **Index of every page**: <https://langfuse.com/llms.txt>, with per-section indexes [llms-docs.txt](https://langfuse.com/llms-docs.txt), [llms-integrations.txt](https://langfuse.com/llms-integrations.txt), and [llms-self-hosting.txt](https://langfuse.com/llms-self-hosting.txt).

### Before writing Langfuse code

- **Install the [Langfuse Agent Skill](https://langfuse.com/docs/api-and-data-platform/features/agent-skill).** It encodes Langfuse's own best practices for instrumentation, prompt management, and evaluation, and materially improves results.
- **Read [What does a good trace look like?](https://langfuse.com/docs/observability/best-practices.md)** before instrumenting an application.
- **Verify endpoints, parameters, and response fields** against the [API reference](https://api.reference.langfuse.com) instead of inferring them from code examples.
- **Use the [Langfuse CLI](https://langfuse.com/docs/api-and-data-platform/features/cli)** (`npx langfuse-cli api <resource> <action>`) to read or write traces, prompts, datasets, and scores from the terminal.

Found an error in these docs? Please open an issue at <https://github.com/langfuse/langfuse-docs/issues>.
