"Structured Outputs is a new capability in the Chat Completions API and Assistants API that guarantees the model will always generate responses that adhere to your supplied JSON Schema. In this cookbook, we will illustrate this capability with a few examples.\n",
"Structured Outputs can be enabled by setting the parameter `strict: true` in an API call with either a defined response format or function definitions.\n",
"Function calling remains similar, but with the new parameter `strict: true`, you can now ensure that the schema provided for the functions is strictly followed.\n",
"If you used JSON mode or function calls before, you can think of Structured Outputs as a foolproof version of this.\n",
"\n",
"This can enable more robust flows in production-level applications, whether you are relying on function calls or expecting the output to follow a pre-defined structure.\n",
"More generally, anything that requires fetching data, taking action, or that builds upon complex workflows could benefit from using Structured Outputs."
"In this example, we want to build a math tutoring tool that outputs steps to solving a math problem as an array of structured objects.\n",
"\n",
"This could be useful in an application where each step needs to be displayed separately, so that the user can progress through the solution at their own pace."
"{\"steps\":[{\"explanation\":\"Start by isolating the term with the variable. Subtract 7 from both sides to do this.\",\"output\":\"8x + 7 - 7 = -23 - 7\"},{\"explanation\":\"Simplify both sides. On the left side, 7 - 7 cancels out, and on the right side, -23 - 7 equals -30.\",\"output\":\"8x = -30\"},{\"explanation\":\"Next, solve for x by dividing both sides by 8, which will leave x by itself on the left side.\",\"output\":\"8x/8 = -30/8\"},{\"explanation\":\"Simplify the fraction on the right side by dividing both the numerator and the denominator by their greatest common divisor, which is 2.\",\"output\":\"x = -15/4\"}],\"final_answer\":\"x = -15/4\"}\n"
"The new version of the SDK introduces a `parse` helper to provide your own Pydantic model instead of having to define the JSON schema. We recommend using this method if possible."
"[Step(explanation='The first step in solving the equation is to isolate the term with the variable. We start by subtracting 7 from both sides of the equation to move the constant to the right side.', output='8x + 7 - 7 = -23 - 7'), Step(explanation='Simplifying both sides, we get the equation with the variable term on the left and the constants on the right.', output='8x = -30'), Step(explanation='Now, to solve for x, we need x to be by itself. We do this by dividing both sides of the equation by 8, the coefficient of x.', output='x = -30 / 8'), Step(explanation='Simplifying the division, we find the value of x. -30 divided by 8 simplifies to the fraction -15/4 or in decimal form, -3.75.', output='x = -15/4')]\n",
"When using Structured Outputs with user-generated input, the model may occasionally refuse to fulfill the request for safety reasons.\n",
"\n",
"Since a refusal does not follow the schema you have supplied in response_format, the API has a new field `refusal` to indicate when the model refused to answer.\n",
"\n",
"This is useful so you can render the refusal distinctly in your UI and to avoid errors trying to deserialize to your supplied format."
"In this example, we will ask the model to summarize articles following a specific schema.\n",
"\n",
"This could be useful if you need to transform text or visual content into a structured object, for example to display it in a certain way or to populate database.\n",
"Summary: Convolutional Neural Networks (CNNs) are deep neural networks used for processing structured grid data like images, revolutionizing computer vision.\n",
"- Convolutional Layers: These layers apply learnable filters to input data to produce feature maps that detect specific features like edges and patterns.\n",
"- Pooling Layers: Also known as subsampling layers, they reduce the spatial dimensions of feature maps, commonly using max pooling to retain important features while reducing size.\n",
"- Fully Connected Layers: These layers connect every neuron in one layer to every neuron in the next, performing the final classification or regression task.\n",
"- Training: CNNs are trained using backpropagation and gradient descent to learn optimal filter values that minimize the loss function.\n",
"- Applications: CNNs are used in image classification, object detection, medical image analysis, and image segmentation, forming the basis of many state-of-the-art computer vision systems.\n",
"Description: Convolutional Neural Networks (CNNs) are a type of deep learning model designed to process structured grid data, such as images, by using layers of convolutional, pooling, and fully connected layers to extract and classify features.\n",
"- Transformer Architecture: A neural network architecture that allows for highly parallelized processing and generation of text, featuring components like embeddings, transformer blocks, attention mechanisms, and decoders.\n",
"- Pre-training and Fine-tuning: The two-stage training process for LLMs, where models are first trained on large text corpora to learn language patterns, followed by task-specific training on labeled datasets.\n",
"- Applications of LLMs: LLMs are used in text generation, machine translation, summarization, sentiment analysis, and conversational agents, enhancing human-machine interactions.\n",
"Description: Large Language Models (LLMs) leverage transformer architecture to process and generate human language, significantly advancing natural language processing applications such as translation, summarization, and conversational agents.\n",
"Summary: Mixture of Experts (MoE) is a machine learning technique that improves model performance by combining predictions from multiple specialized models.\n",
"- Experts: Individual models trained to specialize in different parts of the input space or specific aspects of the task.\n",
"- Gating Network: A network responsible for dynamically selecting and weighting the outputs of experts for a given input.\n",
"- Combiner: Aggregates the outputs from selected experts, weighted by the gating network, to produce the final model output.\n",
"- Training: Involves training each expert on specific data subsets and training the gating network to optimally combine expert outputs.\n",
"- Applications: MoE models are used in natural language processing, computer vision, speech recognition, and recommendation systems to improve accuracy and efficiency.\n",
"Description: Mixture of Experts (MoE) is a machine learning framework that enhances model performance by integrating the outputs of multiple specialized models, known as experts, through a gating network that dynamically selects and weights their contributions to the final prediction.\n",
"Whether you've used JSON mode or function calling before and you want more robustness in your application, or you're just starting out with structured formats, we hope you will be able to apply the different concepts introduced here to your own use case!\n",