Repository: wandb/wandb

Issue:
[CLI]: unable to link artifact to model registry
### Describe the bug

<!--- Description of the issue below  -->
I'm trying to link an artifact to model registry, but I keep getting a strange error:
wandb: ERROR Error while calling W&B API: failed to map client id (<Response [404]>)

What am I doing wrong?

<!--- A minimal code snippet between the quotes below  -->
```python
torch.save(model.state_dict(), 'model.pt')

# artifact
model_artifact = wandb.Artifact('resnet18_model', 'model')
model_artifact.add_file('model.pt')
wandb.run.link_artifact(
    model_artifact,
    'sn4kebyte/test-torchvision/Image Classifier',
)
```

<!--- A full traceback of the exception in the quotes below -->
```shell
wandb: ERROR Error while calling W&B API: failed to map client id (<Response [404]>)
wandb: Waiting for W&B process to finish... (success).
wandb:                                                                                
wandb: 
wandb: Run history:
wandb:  train_acc ▁▇▆█▅
wandb: train_loss █▂▅▁▅
wandb:    val_acc █▆▁▁▃
wandb:   val_loss ▁▄▆█▅
wandb: 
wandb: Run summary:
wandb:  train_acc 0.78689
wandb: train_loss 0.52004
wandb:    val_acc 0.86275
wandb:   val_loss 0.31772
wandb: 
wandb: Synced faithful-sea-36: https://wandb.ai/sn4kebyte/test-torchvision/runs/o7x5w3ts
wandb: Synced 5 W&B file(s), 3 media file(s), 2 artifact file(s) and 0 other file(s)
wandb: Find logs at: ./wandb/run-20220812_171732-o7x5w3ts/logs
```


### Additional Files

_No response_

### Environment

WandB version: 0.13.1

OS: macOS Monterey 12.0.1

Python version: 3.8.9

Versions of relevant libraries:
torch 1.12.1
torchvision 0.13.1

### Additional Context

_No response_


Discussion:
Hi @SN4KEBYTE, thanks for reporting! You can currently only link logged artifacts. Adding the following line to your code snippet should work:

```python
# artifact
model_artifact = wandb.Artifact('resnet18_model', 'model')
model_artifact.add_file('model.pt')
model_artifact = wandb.run.log_artifact(model_artifact) # <-------------------
wandb.run.link_artifact(
    model_artifact,
    'sn4kebyte/test-torchvision/Image Classifier',
)
```

**Future Plans**
In the near future, we will release a fix so you don't have to log before linking. (If it's not logged yet, we'll log it for you and then link.)

**Linking in the Web App/UI** 
Note that you can also use our web app to link a model artifact:

<img width="1766" alt="Screen Shot 2022-08-12 at 10 54 17 AM" src="https://user-images.githubusercontent.com/8609620/184415862-7851baf8-cf70-4e53-93f1-38b637a55522.png">

I hope this helps!


Thanks a lot @vwrj for your quick and helpful response! Everything works fine now.

Resolving patch:
diff --git a/wandb/sdk/wandb_run.py b/wandb/sdk/wandb_run.py
--- a/wandb/sdk/wandb_run.py
+++ b/wandb/sdk/wandb_run.py
@@ -2459,6 +2459,8 @@ def link_artifact(
             aliases = []
 
         if self._backend and self._backend.interface:
+            if isinstance(artifact, Artifact) and not artifact._logged_artifact:
+                artifact = self._log_artifact(artifact)
             if not self._settings._offline:
                 self._backend.interface.publish_link_artifact(
                     self,


Test patch:
diff --git a/tests/functional_tests/t0_main/artifacts/link-artifact.py b/tests/functional_tests/t0_main/artifacts/link-artifact.py
--- a/tests/functional_tests/t0_main/artifacts/link-artifact.py
+++ b/tests/functional_tests/t0_main/artifacts/link-artifact.py
@@ -1,3 +1,7 @@
+"""
+Test for run.link_artifact()
+Allows user to link an artifact to a portfolio, without logging it first
+"""
 import tempfile
 
 import wandb
@@ -13,7 +17,6 @@ def main():
 
             local_path = f"{tmpdir}/boom.txt"
             artifact.add_file(local_path, "test-name")
-            artifact = run.log_artifact(artifact)
             run.link_artifact(artifact, "entity/project/test_portfolio")