Skip to main content
The translation tool for chat completions allows you to translate text during a conversation. While Palmyra X models can perform translation tasks, they are not optimized for these tasks and may not perform well without correct prompting. Palmyra Translate is a dedicated model optimized for translation use cases. This guide explains how to use the translation tool in a chat completion and provides an example of how to use it.
You need an API key to access the Writer API. Get an API key by following the steps in the API quickstart.We recommend setting the API key as an environment variable in a .env file with the name WRITER_API_KEY.

Tool structure

The translation tool allows you to translate text during a chat completion. To use the translation tool, add it to the tools array in your chat-completion endpoint request. The translation tool object has the following structure:
You can only pass one prebuilt tool in the tools array at a time. However, you can pass multiple custom tools in the same request.Prebuilt tools are:

Response format

For non-streaming responses, the translated text is in the choices[0].message.content field. For streaming responses, the translated text is in the choices[0].delta.content field. The response also contains a translation_data field that contains the following information:
You can use the translation tool with streaming chat responses, but the translation tool call is not streamed. The response comes back in the choices[0].delta.content field once the full translation is complete.If you are unfamiliar with the chat completions endpoint or streaming vs. non-streaming responses, learn more in the chat completion guide.
See the chat completion endpoint for more information on the response fields.

Usage example

This example uses palmyra-translate to translate a message during a chat completion.

Create a tools array containing a translation tool

First, create a tools array that specifies the translation tool you want to use.

Send the request using chat completions

Add the tools array to the chat endpoint call along with your array of messages. Setting tool_choice to auto allows the model to choose when to use the translation tool, based on the message provided in the messages array. The response contains the translated text in the choices[0].message.content field.
If you want to verify the translation data in the response, you can print the translation_data field to see the source text, source language code, and target language code that the translation tool used.
By following this guide, you can use the translation tool to have the palmyra-translate model translate a message during a chat completion.

Next steps

Learn about additional capabilities of the Writer API, such as analyzing images and web search.