Some texts (or images) belong to more than one category—think hashtags, product facets, or moderation flags. With LLMs you can ask for all applicable labels in one go.

Basic Multi-Label Example

In the example below, we provide a news headline and ask the model to return all relevant labels from a predefined list—such as space, science, technology, and climate. Multiple labels can be assigned to a single input. The special label none is included to indicate when no categories are applicable, which is great in cases where the input is not relevant to any of the labels.

curl https://api.inference.net/v1/chat/completions \
  -H "Authorization: Bearer $INFERENCE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "meta-llama/llama-3.2-11b-instruct/fp-16",
    "messages": [
      { "role": "system", "content": "You are a multi-label classifier. Given a headline and a list of possible labels, return all applicable labels as an array. Only include labels from the provided list. If none apply, return [\"none\"]. Respond only with the array of matching labels." },
      { "role": "user", "content": "Headline: \"Russia funds new AI climate project\"" }
    ],
    "response_format": {
      "type": "json_schema",
      "json_schema": {
        "name": "news_tags",
        "strict": true,
        "schema": {
          "type": "object",
          "properties": {
            "labels": {
              "type": "array",
              "items": { "type": "string", "enum": ["space","science","technology","climate", "none"] }
            }
          },
          "required": ["labels"],
          "additionalProperties": false
        }
      }
    }
  }'

Common Patterns & Best Practices

Best Practices: DO

  • Use specific, non-overlapping labels
    Example: "python_programming" instead of just "programming"

  • Include a label such as “none” or “other”
    This helps handle cases where no standard label applies

  • Test with edge cases
    Try empty text, very long text, or mixed-language inputs to ensure robustness

Common Pitfalls: DON’T

  • Don’t use too many labels
    For best accuracy, keep the label set under 20–30 options

  • Don’t mix abstraction levels
    Avoid combining broad and narrow categories (e.g., "animal" and "dog") in the same set

  • Don’t forget to handle empty results
    Some content may not match any label; your code should account for this possibility