Skip to content

OpenAI Endpoint

Dev Notes

This class makes use of langchain's cost tracking for openai models. Changes to the involved classes will need to be adapted here. The important classes are:

  • langchain.schema.LLMResult
  • langchain.callbacks.openai_info.OpenAICallbackHandler

Changes for openai 1.0

  • Previously we instrumented classes openai.* and their methods create and acreate. Now we instrument classes openai.resources.* and their create methods. We also instrument* and their create. To be determined is the instrumentation of the other classes/modules under openai.resources.

  • openai methods produce structured data instead of dicts now. langchain expects dicts so we convert them to dicts.



Bases: SerialModel

A wrapper for openai clients.

This class allows wrapped clients to be serialized into json. Does not serialize API key though. You can access openai.OpenAI under the client attribute. Any attributes not defined by this wrapper are looked up from the wrapped client so you should be able to use this instance as if it were an openai.OpenAI instance.

REDACTED_KEYS class-attribute
REDACTED_KEYS: List[str] = ['api_key', 'default_headers']

Parameters of the OpenAI client that will not be serialized because they contain secrets.

client class-attribute instance-attribute
client: Union[OpenAI, AzureOpenAI] = Field(exclude=True)

Deserialized representation.

client_cls instance-attribute
client_cls: Class

Serialized representation class.

client_kwargs instance-attribute
client_kwargs: dict

Serialized representation constructor arguments.


Bases: Endpoint

OpenAI endpoint. Instruments "create" methods in openai client.


openai client to use. If not provided, a new client will be created using the provided kwargs.

TYPE: Optional[Union[OpenAI, AzureOpenAI, OpenAIClient]] DEFAULT: None


arguments to constructor of a new OpenAI client if client not provided.

TYPE: dict DEFAULT: {}