> ## Documentation Index
> Fetch the complete documentation index at: https://docs.galileo.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Log Using the OpenAI Wrapper

> Learn how to integrate and use OpenAI's API with Galileo's wrapper client

{/*<!-- markdownlint-enable MD044 -->*/}

## Overview

When working with OpenAI's API, it's important to set up your environment and client correctly to ensure secure and efficient API calls. This guide shows you how to create a basic integration using Galileo's OpenAI client wrapper.

In this guide you will:

1. [Set up a project with Galileo](#install-dependencies)

1) [Create a chat client using the Galileo OpenAI wrapper](#create-a-chat-client-using-the-galileo-openai-wrapper)

<Note>
  The Galileo OpenAI wrapper currently only supports the synchronous chat completions API.
</Note>

## Before you start

To complete this how-to, you will need:

* An [OpenAI API key](https://openai.com/api/)
* A [Galileo project](/concepts/projects) configured
* Your [Galileo API key](https://app.galileo.ai/settings/api-keys)

{/*<!-- markdownlint-enable MD044 -->*/}

## Install dependencies

To use Galileo, you need to install some package dependencies, and configure environment variables.

<Steps>
  <Step title="Install Required Dependencies">
    Install the required dependencies for your app. If you are using Python, create a virtual environment using your preferred method, then install dependencies inside that environment:

    <CodeGroup>
      ```bash Python theme={null}
      pip install "galileo[openai]" python-dotenv
      ```

      ```bash TypeScript theme={null}
      npm install galileo dotenv
      ```
    </CodeGroup>
  </Step>

  <Step title="Create a .env file, and add the following values">
    <CodeGroup>
      ```ini .env theme={null}
      # Your Galileo API key
      GALILEO_API_KEY="your-galileo-api-key"

      # Your Galileo project name
      GALILEO_PROJECT="your-galileo-project-name"

      # The name of the Log stream you want to use for logging
      GALILEO_LOG_STREAM="your-galileo-log-stream"

      # Provide the console url below if you are using a
      # custom deployment, and not using the free tier, or app.galileo.ai.
      # This will look something like “console.galileo.yourcompany.com”.
      # GALILEO_CONSOLE_URL="your-galileo-console-url"

      # OpenAI properties
      OPENAI_API_KEY="your-openai-api-key"

      # Optional. The base URL of your OpenAI deployment.
      # Leave this commented out if you are using the default OpenAI API.
      # OPENAI_BASE_URL="your-openai-base-url-here"

      # Optional. Your OpenAI organization.
      # OPENAI_ORGANIZATION="your-openai-organization-here"
      ```
    </CodeGroup>

    <Note>
      This assumes you are using a free Galileo account. If you are using a custom deployment, then you will also need to add the URL of your Galileo Console:

      ```ini .env theme={null}
      GALILEO_CONSOLE_URL=your-Galileo-console-URL
      ```
    </Note>
  </Step>
</Steps>

## Create a chat client using the Galileo OpenAI wrapper

<Steps>
  <Step title="Create a file for your application called app.py or app.ts." />

  <Step title="Add code to call OpenAI">
    Add the following code to your application file:

    <CodeGroup>
      ```python Python theme={null}
      from galileo.openai import openai
      from dotenv import load_dotenv

      # Load the Galileo and OpenAI environment variables
      load_dotenv()

      # Create the Galileo wrapped OpenAI client
      client = openai.OpenAI()

      # Define a prompt
      prompt = "Explain the following topic succinctly: Newton's First Law"

      # Get a response from OpenAI
      response = client.chat.completions.create(
          model="gpt-4",
          messages=[{"role": "user", "content": prompt}],
      )

      # Print the response
      print(response.choices[0].message.content.strip())
      ```

      ```typescript TypeScript theme={null}
      import { OpenAI } from "openai";
      import { init, flush, wrapOpenAI } from "galileo";
      import dotenv from "dotenv";
      dotenv.config();

      // Initialize Galileo
      init({
        projectName: process.env.GALILEO_PROJECT,
        logstream: process.env.GALILEO_LOG_STREAM
      });

      // Create the OpenAI wrapper
      const openai = wrapOpenAI(new OpenAI());

      // Define a prompt
      const prompt = "Explain the following topic succinctly: Newton's First Law";

      // Get a response from OpenAI
      const response = await openai.chat.completions.create({
        model: "gpt-4.1-mini",
        messages: [{ content: prompt, role: "user" }],
      });

      // Print the response
      console.log(response.choices[0].message.content)

      // Flush logs before exiting
      await flush({
        projectName: process.env.GALILEO_PROJECT,
        logstream: process.env.GALILEO_LOG_STREAM
      });
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the app">
    <CodeGroup>
      ```bash Python theme={null}
      python app.py
      ```

      ```bash TypeScript theme={null}
      npx tsx app.ts
      ```
    </CodeGroup>

    When the app runs, the span will be logged automatically, with the input as the `prompt`, the output as the returned `response`. The duration and number of tokens will also be logged.
  </Step>

  <Step title="View the logged trace">
    From the [Galileo Console](https://app.galileo.ai), open the Log stream for your project. You will see a trace with a single span containing the logged function call.
  </Step>
</Steps>

Your logging is now set up! You are ready to configure metrics for your project.

## See also

* [Configure metrics](/concepts/metrics/overview)
* [Log streams](/sdk-api/logging/logging-basics)
